MATLAB Answers

Sorting Variables by Value

4 views (last 30 days)
I have a series of solved values I'd like to have sorted. For example,
Apple = (Apple_weight in pounds) / 2.2
Bannana = (Bannana_weight in pounds) / 2.2
Orange = (Orange_weight in pounds) / 2.2
I'd like to have these sorted by mass. In this case it doesn't matter what the mass of the apple is, just whether it is more or less than an orange. How do I get back Orange, Apple, Bannana?
*Side note: I really wish Matlab handled units*

  4 Comments

Show 1 older comment
Charles Rambo
Charles Rambo on 18 May 2020
Read about it, it returns numbers.
A = 45
B = 54
C = 23
>> m=[A,B,C]
m =
45 54 23
>> sort(m,'descend')
ans =
54 45 23
Charles Rambo
Charles Rambo on 18 May 2020
@Stephen, Cool thanks.

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 18 May 2020
Edited: John D'Errico on 18 May 2020
You read about sort. Great. READ ALL ABOUT SORT. And get used to working with arrays, with vectors, rather than named variables, with each data point in a separate variable. MATLAB is a language that uses matrices. Things work better in MATLAB once you start to use vectors and matrices, because now you can work on an entire set of data in one operation.
fruitnames = {'Apple', 'Banana', 'Orange'};
% Just some random numbers, since I don't have a scale handy, nor an orange
% So these are just wild guesses at the weight in pounds of some phantasmic fruits
fruitweight = [.5 .4 .6];
fruitmass = fruitweight/2.205; % 2.205 pounds is roughly one kilogram mass on earth
[~,sortind] = sort(fruitmass,'descend');
fruitnames(sortind)
ans =
1×3 cell array
{'Orange'} {'Apple'} {'Banana'}
And since it is now almost time for breakfast here, all this talk about fruit is making me hungry. :) Thankfully, we have both apples and bananas available at home today, so I'll even have a choice.

  2 Comments

Charles Rambo
Charles Rambo on 18 May 2020
I guess that is most similar to this is the last entry in the description, but since their example sorts a series of numeric variables numerically it doesn't really show how it could be used for this. Hope you have enjoyed your breakfast, I can now sell my fruits across the Bhurma boarder (only other country besides the obvious one still not able to accept the metric system).
John D'Errico
John D'Errico on 18 May 2020
Strangely, I had blackberries with my breakfast. They were quite light of course. But I had multiple berries, so how did the total weight of the blackberries compare to say, a banana? As Steve suggests, a table might be useful, but I did eat my breakfast at a table. I'm getting so confused. It may be lunchtime before I get this all straightened out. ;-)

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 18 May 2020
Rather than storing your data in individually named variables, consider a table.
rng default
weights = randi(10, 3, 1);
names = ["apple"; "banana"; "coconut"];
fruits = table(weights, 'RowNames', names)
sortedFruits = sortrows(fruits)
If you had other data in your fruits table you could select which variable you want to use to sort. sortrows also accepts an input to control whether to sort ascending or descending.
fruits.age = randi(7, 3, 1)
sortedByWeight = sortrows(fruits, "weights")
sortedByAge = sortrows(fruits, "age")
sortedByDecreasingAge = sortrows(fruits, "age", "descend")

  1 Comment

Charles Rambo
Charles Rambo on 19 May 2020
Wow, I will give this a shot. It goes beyond what I need, but I can see how it would be good to know for the future.

Sign in to comment.