Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab: Sorting rows in ascending order but tracking original index

Tags:

matlab

I have an array 'A' of size 50 x 10 filled with doubles.

I want to arrange each row in ascending order to get a new array 'B' but I want to create a third array 'C' where I keep track of the index from original array 'A'

E.g.

Array A (original array):

11   9   13   10
12   4   1    6  
13   5   12   12 

Array B (rearranged array):

9   10   11   13
1   4    6    12
5   12   12   13

Array C (tracking index from array A):

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

Thanks

like image 625
user2707748 Avatar asked Aug 28 '13 19:08

user2707748


2 Answers

Have you tried sort:

[b, c] = sort(A,2)
like image 53
pabaldonedo Avatar answered Oct 20 '22 00:10

pabaldonedo


The second output of sort is exactly what you want.

[B, C] = sort(A, 2);
like image 34
shoelzer Avatar answered Oct 19 '22 23:10

shoelzer