2
votes

I've got a matrix (n x m). And I'd like to know, for each row, the indexes of the coloums that contain the first two maximum values:

2 3 4 2
2 4 7 1
1 1 2 4
5 5 9 6
1 4 2 1
9 8 1 2

The answer should be:

2 3
2 3
3 4
3 4
2 3
1 2

How can I obtain it with matlab commands? I'd like not to use for loops. I tried with:

[x,y]=max(matrix')
y=y';

y gives me the colum indexes for the maximum elements. Now I'd set to zero these elements and repeat the instructions but I have no idea how to do. I treid:

matrix(:,y)=0;

but it doesn't work.

1

1 Answers

6
votes

if A is your matrix, then sort and pick the top two indices,

 [a ix]=sort(A,2)
 ans= ix(:,end-1:end)