user494461
user494461

Reputation:

How to sort a n by 1 dimensional matrix and after sorting get the initial index of a value?

How to sort a list and get the initial index of a value in matlab.

e.g
orignal A=[40;30;20;50;60]
sorted A=[20;30;40;50;60]
indices of sorted A in orignal A =[3;2;1;4;5] 

Upvotes: 1

Views: 64

Answers (2)

George
George

Reputation: 3845

Simple:

[sorted, indices] = sort(A);

Upvotes: 1

Andrey Rubshtein
Andrey Rubshtein

Reputation: 20915

You can use the second output:

   [~,i]=sort([40;30;20;50;60])

The tilda means that the first output is ignored.

Upvotes: 0

Related Questions