ivory
ivory

Reputation: 253

Getting indices of sorted elements of matrix

I am trying to get the sort of row and column according to the value of matrix. for example, if the matrix is

A = [3 4 7; 9 8 6; 2 1 5]

it should output

2 1
2 2
1 3
2 3
3 3 
1 2
1 1
3 1
3 2

I think that should be simple, but I do not have an idea about how to handle that.

Upvotes: 0

Views: 83

Answers (1)

Autonomous
Autonomous

Reputation: 9075

Yes it is indeed very simple.

%sort the vector instead of matrix to get linear indices
[~,ind]=sort(A(:),'descend')  

%convert the linear indices to [row,col] subscripts
[I,J]=ind2sub(size(A),ind)

%display desired answer 
[I J]

To delete rows which have same value in both the columns:

A(A(:,1)==A(:,2),:)=[]

Upvotes: 4

Related Questions