Sort cell array according to the max value in the second row

I wrote scripe that store data in the following format:
row1= {car1, car2, car3,....., etc}
row2 = {20, 180,500, ......., etc}
I want to sort my output as using the maximum value in the second row
I expect this output
row1={car3,car2,car1, .....do this until the end}
row2={500,180, 20,..... do this until the end}
I appreciate your help

1 Comment

Why are you using cell arrays instead of regular numerical arrays? I see no need for complicating it like that. And instead of the unclear/ambiguous "according to the max value" I think most people would say "in descending order".

Sign in to comment.

 Accepted Answer

row1= {1, 2, 3}
row2 = {20, 180,500}
a=sortrows([row1' row2'],2);
out=flipud(a(:,1))
%or
[idx,idx]= sort(cell2mat(row2),'descend')
out=row1(idx)

11 Comments

Thanx Azzi, it doesnt sort the data... Note that my two rows are stored in a variable
Can you give a short example how they are stored. Or just adapt my code to your problem
Azzi,
the ouput I want should look like this
row1={car3,car2,car1, .....do this until the end}
row2={500,180, 20,..... do this until the end}
To work with numerical array, I think it is a not a problem even if they are cell array. but as you can see I have strings in my cell array and cant
row1={'car3','car2','car1'}
row2={500,180, 655}
[idx,idx]=sort(cell2mat(row2),'descend')
out=row1(idx)
No we could not see that you had strings. We had no idea car1, etc. were strings. Those variables could have been any class: doubles, structures, integers, strings, or anything . If you meant literal strings like Azzi showed, then you should have put single quotes around the words. His code will work in that case. If you want to sort row2 also, like you showed, then simply add another line to sort row2.
row1 = {'car3', 'car2', 'car1'}
row2 = {500, 180, 655}
[sortedValues, sortIndexes] = sort(cell2mat(row2), 'descend')
outRow1 = row1(sortIndexes)
outRow2 = row2(sortIndexes)
Thank you Image Analyst but the values in row2 are varied. like
row2 = {20,1999,233}
thank you
this is the result of the
cellmat = 500180665
row1 = {'car1', 'car2', 'car3'}
row2 = {20,1999,233}
[sortedValues, sortIndexes] = sort(cell2mat(row2), 'descend')
outRow1 = row1(sortIndexes)
outRow2 = row2(sortIndexes)
In the command window:
row1 =
'car1' 'car2' 'car3'
row2 =
[20] [1999] [233]
sortedValues =
1999 233 20
sortIndexes =
2 3 1
outRow1 =
'car2' 'car3' 'car1'
outRow2 =
[1999] [233] [20]
Any problem with that?
@Sami: Please post your input data exactly. If you get cellmat = 500180665, the input data are obviously strings:
rows = {'500', '180', '665'}
This differs remarkably from numerical data:
rows = {500, 180, 665}
@Image Analyst: Thank u, I think you understand my problem quite well, my second row output is not like you showed: it is like
row2=
'20' '1999' '233'
@Jan Simon, you are absolutely right, the input data are strings

Sign in to comment.

More Answers (2)

If you want to use a regular numerical array instead of a cell array, you can do this:
% Create Sample data
m = randi(99, 2, 10)
[sortedValues, sortingIndexes] = sort(m(2,:), 'descend')
sorted_m = m(:, sortingIndexes)
In the command window, you'll see:
m =
81 13 63 28 95 16 95 80 42 79
90 91 10 55 96 97 49 15 91 95
sortedValues =
97 96 95 91 91 90 55 49 15 10
sortingIndexes =
6 5 10 2 9 1 4 7 8 3
sorted_m =
16 95 79 13 42 81 28 95 80 63
97 96 95 91 91 90 55 49 15 10

2 Comments

Thank you Image Analyst. but I have to use the cell array and the string therein. I try to change the to cell2mat and str2num. but it seems not working...but thank you
Is this solved yet? I can't tell from your comments, where you say "@Image Analyst: Thank u, I think you understand my problem quite well". Did the code solve your problem of sorting both arrays in the same order, or not?
row1 = {'car1', 'car2', 'car3'}
row2 = {20,1999,233}
[sortedValues, sortIndexes] = sort(cell2mat(row2), 'descend')
outRow1 = row1(sortIndexes)
outRow2 = row2(sortIndexes)
You didn't mark any answer as "Accepted" yet, so I'm guessing it's not working. If not, then please attach your actual m-file so we can get this solved.

Sign in to comment.

Thank you Azzi, Image Analyst, Jan Simon, I managed to solve the problem using the cellfun fucntion, as Jan Simon mentioned, they are remarkably different. I use the cellfun funtion to change the string cell array to a numeric cell array. Thank you

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!