Sorting Variables by Value

조회 수: 4 (최근 30일)
Charles Rambo
Charles Rambo 2020년 5월 18일
댓글: Charles Rambo 2020년 5월 19일
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
Stephen23
Stephen23 2020년 5월 18일
Charles Rambo
Charles Rambo 2020년 5월 18일
@Stephen, Cool thanks.

댓글을 달려면 로그인하십시오.

채택된 답변

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에서 Multibody Modeling에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by