MIN Minimum Function
Section: Elementary Functions
Usage
Computes the minimum of an array along a given dimension, or alternately, computes two arrays (entry-wise) and keeps the smaller value for each array. As a result, themin
function has a number of syntaxes. The first
one computes the minimum of an array along a given dimension.
The first general syntax for its use is either
[y,n] = min(x,[],d)
where x
is a multidimensional array of numerical type, in which case the
output y
is the minimum of x
along dimension d
.
The second argument n
is the index that results in the minimum.
In the event that multiple minima are present with the same value,
the index of the first minimum is used.
The second general syntax for the use of the min
function is
[y,n] = min(x)
In this case, the minimum is taken along the first non-singleton
dimension of x
. For complex data types,
the minimum is based on the magnitude of the numbers. NaNs are
ignored in the calculations.
The third general syntax for the use of the min
function is as
a comparison function for pairs of arrays. Here, the general syntax is
y = min(x,z)
where x
and z
are either both numerical arrays of the same dimensions,
or one of the two is a scalar. In the first case, the output is the
same size as both arrays, and is defined elementwise by the smaller of the
two arrays. In the second case, the output is defined elementwise by the
smaller of the array entries and the scalar.
Function Internals
In the general version of themin
function which is applied to
a single array (using the min(x,[],d)
or min(x)
syntaxes),
The output is computed via
and the output array n
of indices is calculated via
In the two-array version (min(x,z)
), the single output is computed as
Example
The following piece of code demonstrates various uses of the minimum function. We start with the one-array version.--> A = [5,1,3;3,2,1;0,3,1] A = 5 1 3 3 2 1 0 3 1
We first take the minimum along the columns, resulting in a row vector.
--> min(A) ans = 0 1 1
Next, we take the minimum along the rows, resulting in a column vector.
--> min(A,[],2) ans = 1 1 0
When the dimension argument is not supplied, min
acts along the first
non-singular dimension. For a row vector, this is the column direction:
--> min([5,3,2,9]) ans = 2
For the two-argument version, we can compute the smaller of two arrays, as in this example:
--> a = int8(100*randn(4)) a = 27 61 -128 -22 -54 -74 -27 -61 66 -128 127 34 -13 -128 42 86 --> b = int8(100*randn(4)) b = 56 33 -15 17 127 127 29 -97 99 -45 117 -128 56 -114 127 104 --> min(a,b) ans = 27 33 -128 -22 -54 -74 -27 -97 66 -128 117 -128 -13 -128 42 86
Or alternately, we can compare an array with a scalar
--> a = randn(2) a = -0.2804 0.3739 -0.8883 -0.4260 --> min(a,0) ans = -0.2804 0 -0.8883 -0.4260