Get the minimum row of a matrix in MATLAB

I have a matrix like:

1.0000   24.6914
2.0000   34.5679
3.0000   27.1605
4.0000   30.8642
5.0000   27.1605
6.0000   25.9259
7.0000   14.6914
8.0000   23.4568
9.0000   25.9259
10.0000  22.2222
 ...       ...
23.0000  23.4568

I know that if I use

min( MATRIX(:,2) )

I get the min value in column 2, but how can I get the min value and the corresponding value from the first column? As an example, my desired result would be:

7.0000   14.6914
+3
source share
2 answers

First you need to set the index to min:

[minVal, minInd] = min( MATRIX(:,2) );

And then access the first row in this index:

MATRIX(minInd,1);

A slightly less elegant syntax would look like this:

MATRIX(find(MATRIX(:,2)==min(MATRIX(:,2)),1));
+6
source
MATRIX(MATRIX(:,2)==min(MATRIX(:,2)),:)
+3
source

Source: https://habr.com/ru/post/1788831/


All Articles