How to find where are the location of the original values after the matrix was sorted

1 view (last 30 days)
Hello,
I have a 6x4 matrix. I am using sort function to sort the values of each row from the smallest to the largest creating a new matrix. Is there a way to locate where each value of the first row is located in the new matrix?
THank you

Accepted Answer

Stephen
Stephen on 27 May 2019
>> old = randi(9,6,4) % fake data
old =
8 6 3 6
1 4 9 4
8 1 4 8
6 3 8 7
3 8 4 1
6 8 8 5
>> [new,idx] = sort(old,2)
new =
3 6 6 8
1 4 4 9
1 4 8 8
3 6 7 8
1 3 4 8
5 6 8 8
idx =
3 2 4 1
1 2 4 3
2 3 1 4
2 1 4 3
4 1 3 2
4 1 2 3
>> [~,idy] = min(idx,[],2)
idy =
4
1
3
2
2
2
  3 Comments

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!