In the following article, we explain how to use the min() function in MATLAB® to find the smallest value among the elements of an array. We then show you a complete description of the main functions and various ways to use this function to process 2D and multidimensional arrays.

This article includes practical examples and pictures to help you better understand how the min() function works in MATLAB.

MATLAB min() Function Syntax

r = min(a)

r = min(a, [ ], dim)


r = min(a, [ ], ‘all’)


r = min(a, [ ], nanflag)


r = min(a, [ ], dim, nanflag)


r = min(a, [ ], vecdim)


r = min(a, [ ], ‘all’, nanflag)


r = min(a, [ ], vecdim, nanflag)

r = min(a, b)


[r, i ] = min(___)


[r, i ] =min(a, [ ], ‘all’, ___)


[r, i ] =min(a, [ ], ___, ‘linear’)

MATLAB min() Function Description and Examples

The min() function returns in “r” the minimum value among all the elements of the array “a”. This function can also return the index of the minimum values sent in “a” and returned in “r.”

min() has various input and output data processing modes. These modes are selected in the function call via flags and dimension vectors.

The type of input arguments that this function accepts are scalar, vector, matrix, and multidimensional arrays. The data types supported by min() are single, double, int8, int16, int32, int64, uint8, uint16, uint32, uint64, logical, categorical, datetime, and duration. In the following examples, we will show you the different ways this function has to obtain the minimum values in an array.

Below, we will detail each of the arguments and input flags of the min() function:

a: This input specifies the input scalar, vector, or matrix. The type of data supported by this input is scalar, vector, matrix, or multidimensional array.

b: Additional input matrix. In cases where min() is sent with an additional array, it must have dimensions compatible with “a”. The data type for this input is the same as for “a”

dim: This input sets the dimension on which the min() function will operate. The data type supported by “dim” is a positive integer scalar.

vecdim: dimension vector. min() returns the maximum value among the elements in the array dimensions specified in this vector.

nanflag: This flag specifies if NaN values should be included in the output array

i: index. In this output, min() returns the index of the maximum values obtained from “a”.

How to Get the Minimum Value Between the Elements of a Vector with the min() Function of MATLAB

In this example, we show you how to determine the element with the smallest value in a row vector. To do this, we create the vector “a” with elements of different values and send it as an input argument to the min() function.

a = [4,13,5,16,53,66,2,16,18,88,15,52,5]


r = min(a)

The min() function searches the values of the individual elements of the array “a” and returns the smallest value in “r”.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/Min-Function-in-MatLab-1.jpg" data-lazy- height="755" src="data:image/svg xml,” width=”1440″>

How to Get the Minimum Element of Each Column with the min Function in MATLAB

As explained above, the min() function has several ways to process input arguments. In this example, we will show you how to find the minimum value of each column in a magic square with 6×6 elements.

The min() function will return the row vector “r” with the minimum value of each column.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/Min-Function-in-MatLab-2.jpg" data-lazy- height="757" src="data:image/svg xml,” width=”1440″>

How to Get the Minimum Element in Each Row of a Matrix Using the min() Function and the “dim” Option in MATLAB

In the above example, the elements of the array were handled column by column. The min() function handles input arguments this way by default. With the “dim” option, it is possible to select any dimension of the matrix to get the minimum value. The syntax is shown below.

If the dim option has the value 1, the min() function returns a row vector “r” with the minimum value of each column. If the dim option has the value 2, it returns a column vector “r” with the minimum value of each row (see below).

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/Min-Function-in-MatLab-3.jpg" data-lazy- height="258" src="data:image/svg xml,” width=”970″>

In this example, we create a 6×6 magic square and find the minimum value, but in this case from each line or in dimension 2.

a = magic(6)


r = min(a, [ ], 2)

As a result, the function min() returns the column vector “r” with the minimum value of each row of the magic square.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/Min-Function-in-MatLab-4.jpg" data-lazy- height="758" src="data:image/svg xml,” width=”1440″>

How to Get the Index of the Minimum Values with the min Function of MATLAB

With the function min(), it is also possible to obtain the location index of each minimum value of “a”. This function returns a vector of the same type and size as “r” with the index of the minimum values of each row or column. The syntax is as follows.

Now, we will see how to obtain the index of the minimum values of each column of a 5×5 magic square.

a = magic(5)

[r, i] = min(a)

As a result, min() returns two row vectors with the index of the minimum elements of “a” and “r”.<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/Min-Function-in-MatLab-5.jpg" data-lazy- height="758" src="data:image/svg xml,” width=”1440″>

How to Get the Minimum Unique Value of an Array Using the “all” option in the min function in MATLAB

With the “all” option of this function, we can obtain the minimum unique value between all the elements of the array. The syntax as follows:

In the following example, we will show you how to find the minimum unique value of an array with 5×5 elements.

a= [ 7, 12, 29, 56, 11;


    55, 22, 18, 68, 48;


    45, 18, 66, 97, 47;


    29, 51, 32, 44, 71]


r = min(a, [], ‘all’)

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/Min-Function-in-MatLab-6.jpg" data-lazy- height="757" src="data:image/svg xml,” width=”1440″>

In this mode, min will return in “r” a scalar with a single value.

How to Include or Exclude NaN Values in the Output Array When Using the min() Function in MATLAB

The min function has the ability to include or exclude NaN values in the input and output arguments by using the “includenan” and “‘omitnan’” flags. By default, this mode excludes NaN values. The syntax for this mode is shown below:

In the following example, we show these two options when handling NaN values. In the first one, these values were included in the output array by the flag “includenan”. In the second one, they were omitted by “omitnan”.

a= [7, 12,29, 56, NaN; 55, 22, 18, NaN, NaN]

r= min(a,[], 1, ‘includenan’)

a= [7, 12,29, 56, NaN; 55, 22, 18, NaN, 57]

r= min(a,[], 1, ‘omitnan’)

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/Min-Function-in-MatLab-7.jpg" data-lazy- height="758" src="data:image/svg xml,” width=”1440″>

Conclusion

In this article, we explained how to use MATLAB’s min() function to find the minimum values of an array, matrix, or vector. We also explained the various options that this function provides when handling input and output data. In this article, we have given some practical examples with different arrays and data types. We have also explained in more detail the input argument types of this function and the type of data accepted. We hope that this MATLAB article was helpful for you. See other Linux Hint articles for more tips and information.