Sorting Variables by Value

17 次查看(过去 30 天)
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 个评论

请先登录,再进行评论。

采纳的回答

John D'Errico
John D'Errico 2020-5-18
编辑:John D'Errico 2020-5-18
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 个评论
Charles Rambo
Charles Rambo 2020-5-18
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 2020-5-18
编辑:John D'Errico 2020-5-18
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. ;-)

请先登录,再进行评论。

更多回答(1 个)

Steven Lord
Steven Lord 2020-5-18
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 个评论
Charles Rambo
Charles Rambo 2020-5-19
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by