MATLAB: Sort matrix by increasing values (with doubles) and return indices to then sort secondary cell structure

duplicatesindexindicesMATLABmatrixsort

Hi everyone,
I'm having trouble finding a way to sort two matrices next to eachother. I have a 27×7 matrix with different weights and an accompanying 27×7 cell structure with measured values according to those weights (determined experimentally). I want to sort the cell structure so I can plot the data in individual plots next to eachother as the weight increases. The weight matrix has a couple of doubles which I need to deal with as well. To visualize I will demonstrate with two 2×3 structures what I mean.
weight = [50 100 150; 70 150 220]
data = [{1} {2} {3}; {4} {5} {6}]
I want to find the indices as they increase, so I want to end up with a matrix with the row number in column 1 and the column number in column 2. The order in which the doubles are read is not important:
ind_weight = [1 1; 2 1; 1 2; 1 3; 2 2; 2 3]
So now I can form a loop to restructure the data into a new 1×6 cell structure:
for x = 1:6
data_ordered{x} = data{ind_weight(x,:)};
weight_ordered{x} = weight(ind_weight(x,:))
end
Now weight_ordered and data_ordered should come out as:
data_ordered = {1}{4}{2}{3}{5}{6}
weight_ordered = [ 50 70 100 150 150 220]
The problem i'm having is I don't know how to properly sort the data or recall the indices of the locations of the data with the duplicates. I was trying to use recall the location of max(max(weight)) and after that setting the value to 0 so it wouldn't pop up again but this is extremely strenuous and sloppy.
I hope anyone can help! Thank you in advance!
Kind regards,
Michiel

Best Answer

You appear to be overthinking things. If you want to sort something, just use sort:
>> [wo,idx] = sort(weight(:));
>> do = data(idx)
do =
[1]
[4]
[2]
[5]
[3]
[6]
>> wo
wo =
50
70
100
150
150
220