HaWa
HaWa

Reputation: 229

How can I extract row whose element in 1st column is the maximum?

I have following data: eg1:

5.34   -32.82    0.71
7.65   -32.74    0.67
9.89   -32.76    0.69
13.81   -32.74    0.68
17.20   -33.27    0.71
22.83   -33.28    0.61
27.10   -33.40    0.67
29.22   -33.20    0.66

Output: Comparing max(:,1), I need to extract 29.22 -33.20 0.66 Eg2:

4.58   -49.98    1.27
5.84   -50.13    1.23
7.33   -50.15    1.24
10.39   -50.18    1.28
13.60   -50.03    1.27
17.73   -50.07    1.34
10.99   -49.90    1.41
10.81   -49.76    1.43

Output should be:17.73 -50.07 1.34 EDIT:

4.58    -49.98  1.27    5.31    -38     1.12
5.84    -50.13  1.23    7.65    -38     1.11
7.33    -50.15  1.24    9.88    -38.01  1.12
10.39   -50.18  1.28    13.78   -38     1.12
13.6    -50.03  1.27    17.2    -38.07  1.13
17.73   -50.07  1.34    22.8    -38.06  1.14
10.99   -49.9   1.41    27.19   -38.06  1.14
10.81   -49.76  1.43    29.29   -38.02  1.13

OUTPUT: executing a(a==max(a(:,1)),:) for each consecutive column

17.73   -50.07  1.34
29.29   -38.02  1.13

Upvotes: 1

Views: 39

Answers (1)

Adriaan
Adriaan

Reputation: 18177

A=sortrows(A);
A(end,:)
ans =
   29.2200  -33.2000    0.6600

sortrows sorts your matrix row-wise, with the maximum element of row one obviously at the end. Therefore, extract the endrow and you're done.

Even faster without sorting your dataset:

A(A==max(A(:,1)),:)

This finds the entry in the first column where A attains its maximum and uses that as a logical index to extract the full row.

Upvotes: 4

Related Questions