Sorting Variables by Value
이전 댓글 표시
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
KSSV
2020년 5월 18일
Read about sort.
Charles Rambo
2020년 5월 18일
편집: Charles Rambo
2020년 5월 18일
Stephen23
2020년 5월 18일
"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
Charles Rambo
2020년 5월 18일
채택된 답변
추가 답변 (1개)
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")
카테고리
도움말 센터 및 File Exchange에서 Multibody Modeling에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!