9

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

Sibbs Gambling
  • 19,274
  • 42
  • 103
  • 174
Rick T
  • 3,349
  • 10
  • 54
  • 119
  • 2
    possible duplicate of [MATLAB - Sort a matrix based off how a vector is sorted](http://stackoverflow.com/questions/13998098/matlab-sort-a-matrix-based-off-how-a-vector-is-sorted) ... and this answer is also a duplicate. These question already appeared a bunch of time, just search for "vector" instead of "array" – Robert Seifert Oct 30 '13 at 14:15
  • short answer: get the sorting indexes from `sort` and [apply them to the other arrays](http://stackoverflow.com/a/2679517/2778484) OR put the vectors as columns of a matrix and [use `sortrows`](http://stackoverflow.com/a/135115/2778484) – chappjc Oct 30 '13 at 16:05

1 Answers1

15
[sorted, indices] = sort(c)
% get your output with
a(indices)
b(indices)
YXD
  • 31,741
  • 15
  • 75
  • 115