Skip to main content
Contents Index
Calc
Dark Mode Prev Next Profile
\(\require{mathtools}\newcommand\DLGray{\color{Gray}}
\newcommand\DLO{\color{BurntOrange}}
\newcommand\DLRa{\color{WildStrawberry}}
\newcommand\DLGa{\color{Green}}
\newcommand\DLGb{\color{PineGreen}}
\newcommand\DLBa{\color{RoyalBlue}}
\newcommand\DLBb{\color{Cerulean}}
\newcommand\ds{\displaystyle}
\newcommand\ddx{\frac{d}{dx}}
\newcommand\os{\overset}
\newcommand\us{\underset}
\newcommand\ob{\overbrace}
\newcommand\obt{\overbracket}
\newcommand\ub{\underbrace}
\newcommand\ubt{\underbracket}
\newcommand\ul{\underline}
\newcommand\tikznode[3][]
{\tikz[remember picture,baseline=(#2.base)]
\node[minimum size=0pt,inner sep=0pt,#1](#2){#3};
}
\newcommand\del{\nabla}
\newcommand\R{\mathbb{R}}
\newcommand\C{\mathcal{C}}
\newcommand\N{\mathcal{N}}
\newcommand\eps{\varepsilon}
\renewcommand\epsilon{\varepsilon}
\renewcommand\subset{\subseteq}
\newcommand\norm[1]{\|{#1}\|}
\newcommand\matrixbrackets[4][1]{
\draw (#3,#2) -- (\fpeval{#3+0.2},#2);
\draw (#3,#1) -- (#3 ,#2);
\draw (#3,#1) -- (\fpeval{#3+0.2},#1);
\draw (#4,#2) -- (\fpeval{#4-0.2},#2);
\draw (#4,#1) -- (#4 ,#2);
\draw (#4,#1) -- (\fpeval{#4-0.2},#1);
}
\tikzstyle{circle node 0}=[fill=white, draw=black, shape=circle, inner sep=0pt]
\tikzstyle{circle node 2}=[fill=white, draw=black, shape=circle, inner sep=2pt]
\tikzstyle{hrect node 0}=[fill=white, draw=black, inner sep=2pt, outer sep=0pt]
\tikzstyle{hrect node 2}=[fill=white, draw=black, inner sep=2pt, outer sep=2pt]
\tikzstyle{hrect node 3}=[fill=white, draw=black, inner sep=2pt, outer sep=3pt]
\tikzstyle{vrect node 0}=[fill=white, draw=black, inner sep=0pt, outer sep=0pt]
\tikzstyle{vrect node 2}=[fill=white, draw=black, inner sep=0pt, outer sep=2pt]
\tikzstyle{vrect node 3}=[fill=white, draw=black, inner sep=0pt, outer sep=3pt]
\tikzstyle{hidden node 0}=[inner sep=0pt, outer sep=0pt]
\tikzstyle{hidden node 2}=[fill=white, inner sep=2pt, outer sep=2pt]
\tikzstyle{rect node 1}=[fill=white, inner sep=2pt, outer sep=0pt]
\tikzstyle{rect node 2}=[fill=white, draw=black, inner sep=2pt, outer sep=0pt]
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Subsection B.3 The min and max functions
The built-in functions
min and
max find the smallest and largest values in data. They are especially useful when you want a quick summary of a vector or matrix without writing a loop.
min and max Syntax.
smallest = min(v) and
largest = max(v)
[smallest, idx] = min(v) and
[largest, idx] = max(v)
colMins = min(A) and
colMaxes = max(A)
For a vector
v, these functions return a single value. For a matrix
A, they work column by column unless you first turn the matrix into one long vector with
A(:).
Vectors. If the input is a vector,
min returns the smallest entry and
max returns the largest entry. With two outputs, MATLAB also returns the position where the extreme value occurs.
🌌 Example 9 . Using min and max with a Vector.
Suppose
v stores several measurements:
v = [12, 5, 19, 8, 19];
x = min(v) % returns 5
[maxVal, idx] = max(v) % returns 19 and 3
The call
min(v) scans the whole vector and returns the smallest value. The call
[maxVal, idx] = max(v) returns two pieces of information: the largest value, and the position of its first occurrence. Here the maximum value
19 appears twice, but MATLAB reports index
3 because that is where the first
19 appears.
A common pattern is to combine the two functions to measure the spread of the data:
dataRange = max(v) - min(v) % returns 14
Matrices. If the input is a matrix, MATLAB applies
min or
max to each column separately. The result is a row vector containing one answer per column.
🌌 Example 10 . Column-wise Results for a Matrix.
A = [4, 9, 2;
7, 3, 8;
1, 6, 5];
colMins = min(A) % returns [1, 3, 2]
[colMaxes, rowIdx] = max(A) % returns [7, 9, 8] and [2, 1, 2]
The first output of
min(A) is a row vector of the column minima. The two-output form of
max(A) returns the maximum value in each column and the row where that maximum occurs. So the largest entry in column 1 is
7 in row
2, the largest entry in column 2 is
9 in row
1, and the largest entry in column 3 is
8 in row
2.
Sometimes you want the extreme value from the entire matrix, not one answer per column. The standard MATLAB idiom is to flatten the matrix with
A(:), which turns every entry into a single column vector.
🌌 Example 11 . Finding the Extreme Value in an Entire Matrix.
Continuing with the same matrix
A:
overallMin = min(A(:)) % returns 1
overallMax = max(A(:)) % returns 9
The expression
A(:) collects all entries of
A into one vector, so
min and
max now work on the entire matrix at once.
Reading Questions Check Your Understanding
1.
(a) Smallest value in a vector.
If
v = [7, 4, 9, 6], what does
min(v) return?
4
min returns the smallest entry in the whole vector. Among 7, 4, 9, and 6, the smallest is 4.
7
7 is the first entry, but min looks through the entire vector, not just the first position.
9
9 is the largest entry, so it is the result of max(v), not min(v).
6
6 is neither the smallest nor the largest entry in the vector.
(b) Index returned by max.
If
v = [2, 11, 5, 11], what does
[maxVal, idx] = max(v) return?
maxVal = 11 and idx = 2
max returns the largest value and the index of its first occurrence. The value 11 appears at positions 2 and 4, so MATLAB reports index 2.
maxVal = 11 and idx = 4
11 is the maximum, but MATLAB reports the first position where it occurs, not the last one.
maxVal = 5 and idx = 3
5 is not the largest value in the vector, so it cannot be the result of max(v).
maxVal = 2 and idx = 1
2 is the first value, not the maximum value.
(c) Matrix results are column-wise.
[5, 8]
For a matrix, max works one column at a time. The maximum of the first column is 5, and the maximum of the second column is 8.
[8, 5]
These are the right numbers in the wrong order. MATLAB reports one answer per column, so the first entry comes from column 1 and the second entry comes from column 2.
8
8 is the largest value in the entire matrix, but max(A) does not flatten the matrix automatically. It returns one maximum per column.
[3, 1]
Those are not the column maxima. In fact, they are the first-row entries of the matrix.
(d) Finding the whole-matrix maximum.
Which expression returns the single largest entry in a matrix
A?
max(A(:))
A(:) turns every entry of the matrix into one long vector, and then max returns the largest value from that vector.
max(A)
max(A) returns a row vector of column maxima, not one overall maximum.
max(size(A))
size(A) describes the shape of the matrix. Its maximum tells you the larger dimension, not the largest entry of the matrix.
A(max)
This is not valid syntax for asking MATLAB to search through the entries of A.
(e) Row indices from matrix max.
A = [4, 2, 9;
7, 5, 1;
3, 8, 6];
what is the second output in
[colMaxes, rowIdx] = max(A)?
[2, 3, 1]
The maxima occur at row 2 in column 1, row 3 in column 2, and row 1 in column 3. The second output records those row locations.
[7, 8, 9]
Those are the maximum values themselves, which belong in the first output, not the second one.
[1, 2, 3]
This would only happen if each maximum appeared on the diagonal, but that is not where the maxima are in this matrix.
[3, 2, 1]
The last two positions are reversed. Check each column separately and record the row where the maximum appears.
(f) Using min and max together.
If
v = [10, 14, 6, 9], what does
max(v) - min(v) compute?
The difference between the largest and smallest values
max(v) gives the top value and min(v) gives the bottom value, so subtracting them measures how spread out the data is.
The sum of all entries in the vector
To add every entry, you would use sum(v), not max(v) - min(v).
The index of the largest value
Indices are only returned when you ask for a second output, such as [largest, idx] = max(v).
The average of the vector
The average is computed with mean(v), not by subtracting the minimum from the maximum.
You have attempted
of
activities on this page.