Matlab

How to Use min() Function in MATLAB

The min() function in MATLAB returns the smallest value among the elements of the array or matrix. The min() function of MATLAB also returns the index of the smallest number of the provided data set. This function accepts the various input arguments including scalar, multidimensional array, vector, and matrix. The min() function quickly and conveniently determines the minimum value among the given data set.

Syntax

The format for using the min() function in MATLAB is given below:

result = min(data)

 

How to Use the min() Function in MATLAB?

There are different ways to use the min() function in MATLAB depending on the data set you are dealing with; a few examples are provided below:

1: MATLAB min() Function to Find the Smallest Element in an Array

The min() function in MATLAB is used to find the smallest possible element in an array. The following example uses the min() function to find the smallest value of the data array.

data = [12,14,18,17,23,56,0,22,18,88,15,52,5]
r = min(data)

 

Running the code will display the value 0 as it is the smallest value in the given array:

2: MATLAB min() Function to Find the Minimum Value in Matrix

In the following example, we have a matrix of 5×5 elements, and we are finding the smallest value in each column of the matrix. The min() function of MATLAB finds the smallest value of the matrix in each column by default:

data = magic(5)
r = min(data)

 

To find the minimum value in each row, you can use the dim parameter with the min() function. The syntax of using the min() function for finding the smallest value of a row is shown below:

r = min(a, [ ], dim)

 

The dim option has different values, you can use according to the need:

 

dim value

Purpose

1 Determine the smallest value from each column of the matrix
2 It is used to find the minimum value of the row

 

In the below sample code, we are finding the minimum value of the row of the 5×5 matrix:

data = magic(5)
r = min(data, [ ], 2)

 

3: MATLAB min() Function to Get the Index of the Minimum Value of a Matrix

If you want to find the minimum value from an array with an index, you can use the following syntax:

[r, i] = min(a)

 

Consider the following sample code to find the index of the element with the smallest value. The r will return the smallest value of each column of the matrix, and i will return the index position of the smallest element:

data = magic(5)
[r, i] = min(data)

 

4: MATLAB min() Function with “all” Parameter

If you want to identify the smallest value from the matrix, you can use the all parameter. The syntax of using the all parameter with the min() function is as follows:

r = min(a, [ ],'all')

 

Consider the following example to find the minimum value from 5×5 elements.

data = magic(5)
r = min(data, [], 'all')

 

Bottom Line

The min() function in MATLAB is an essential tool for finding the minimum value within an array or a matrix. It can quickly find the smallest value from an array or matrix, making it valuable for data analysis, modeling, and simulation tasks. By utilizing the min() function, MATLAB users can easily extract critical information from their data.

About the author

Zainab Rehman

I'm an author by profession. My interest in the internet world motivates me to write for Linux Hint and I'm here to share my knowledge with others.