Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting multiple arrays based on another arrays sorted order

I have three separate arrays in matlab/octave and they are all associated with each other.

I'm trying to sort the array values of a and b based on the sort of c (so when c is sorted, a and b arrays are sorted in the same order as the c array).

Example:
Original Array
a= [1.2   2   3   4    5   6]
b= [3     5   6   4.1  7   9]
c= [2.2   1   9   6    8   3]

Arrays a and b are based on the sort of c (notice all the arrays are sorted based on the order Array c is sorted in)

Final Array that I want:
a= [2   1.2   6   4    5   3]
b= [5   3     9   4.1  7   6]
c= [1   2.2   3   6    8   9]

Aloha Rick

PS: I'm using matlab/octave if there is a better way to do this please let me know

like image 300
Rick T Avatar asked Oct 30 '13 14:10

Rick T


1 Answers

[sorted, indices] = sort(c)
% get your output with
a(indices)
b(indices)
like image 124
YXD Avatar answered Oct 26 '22 08:10

YXD