Sorting Variables by Value
16 views (last 30 days)
Show older comments
Charles Rambo
on 18 May 2020
Commented: Charles Rambo
on 19 May 2020
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
Stephen23
on 18 May 2020
"I really wish Matlab handled units"
Third-party class: https://blogs.mathworks.com/pick/2017/03/31/physical-units-in-matlab/
Symbolic toolbox: https://www.mathworks.com/help/symbolic/units-list.html
Accepted Answer
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
John D'Errico
on 18 May 2020
Edited: 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. ;-)
More Answers (1)
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")
See Also
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!