## 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, the `min` 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 the `min` 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

    -->
    quit

We first take the minimum along the columns, resulting in a row vector.

    --> min(A)

    ans =

     0 1 1

    -->
    quit

Next, we take the minimum along the rows, resulting in a column vector.

    --> min(A,[],2)

    ans =

     1
     1
     0

    -->
    quit

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

    -->
    quit

For the two-argument version, we can compute the smaller of two arrays, as in this example:

    --> a = int8(100*randn(4))

    a =

      -3  59  -5 110
     -14  70 -16  -3
      69 -93   1 118
     -23   0  16 -74

    --> b = int8(100*randn(4))

    b =

       64  -51   74   84
      -40  -62  -84 -126
     -102  -12   43  -54
       69   50  -56   29

    --> min(a,b)

    ans =

       -3  -51   -5   84
      -40  -62  -84 -126
     -102  -93    1  -54
      -23    0  -56  -74

    -->
    quit

Or alternately, we can compare an array with a scalar

    --> a = randn(2)

    a =

       -0.8512   -0.6258
        0.8415    1.3391

    --> min(a,0)

    ans =

       -0.8512   -0.6258
             0         0

    -->
    quit