Matlab: minimal matrix
This can be done simply with find
, where you would use two output versions. So, you want to find those rows and columns in your matrix that match the minimum value in your matrix.
Thus:
[row, col] = find(matrix == min(matrix(:)));
row
and col
will contain the arrangement of rows and columns matrix
that are equal to this minimum value. Note that I had to unwrap the matrix into a vector by doing matrix(:)
. The reason is that if you have to use min
on a matrix, it will by default give you the minimum for each column. Since you want to find the minimum over the entire matrix, you must convert that to one vector and then find the minimum over the entire vector.
Note that this will return the columns of rows and columns of all , which will correspond to a minimum, so it actually provides row
, and col
as a column vector N x 1
, where N
the total number of elements in the matrix
equal to a minimum.
If only one is required , just add 1 as the second parameter to find
:
[row, col] = find(matrix == min(matrix(:)), 1);
source to share