Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I need to order a list that is dependant on another list. how to change both lists?

Tags:

list

matlab

I have a Matlab program that generates a list x = 6.1692 8.1863 5.8092 8.2754 6.0891 the program also outputs another list aspl = 680 637 669 599 693.

The two lists are on equal length and the first element in list x is related to the first element in list aspl. I need to graph the two lists but want list aspl to be in order from smallest to largest. How would I go about doing this? If I need to move the first element in aspl to position 4 in the list, then the first element of list x also needs to be moved to position 4 in list x. The numbers above are not important they are just examples, the actual program generates hundereds of numbers.

for example x = 6.1692 8.1863 5.8092 8.2754 initially

     aspl =  680   637   669   599   693

after changing aspl to ascending order this is how x should look.

x = 5.8092 8.1863 5.8092 6.1692 8.2754

aspl = 599 637 669 680 693

like image 888
Ben Fossen Avatar asked Feb 28 '23 07:02

Ben Fossen


1 Answers

Use the second output of sort:

%# sort aspl, get new order of aspl
[sortedAspl, sortOrder] = sort(aspl);
%# reorder x the same way as aspl
sortedX = x(sortOrder);
like image 182
Jonas Avatar answered May 09 '23 16:05

Jonas