How to sort a table or Excel file (.xlsx) (.csv) and sort the labels correspondingly?

조회 수: 9 (최근 30일)
Dear scholars,
I was wondering how to wrtie a code in MATLAB so sort a table based on the magnitude if numbers and then sort/move the corresponding labels as well?
I mean: suppose: a = 1; b= 2; c=3
I nead a table in this format:
a 1
b 3
c 2
Then I need to sort such a table in an ascending fashion in which sorts the labels (a, b, c) as well. The final answer should be:
a 1
c 2
b 3
Any ideas?

답변 (1개)

Cris LaPierre
Cris LaPierre 2021년 4월 8일
In MATLAB, you would use sortrows.
name = ["a";"b";"c"];
val = [1;3;2];
T = table(name,val)
T = 3×2 table
name val ____ ___ "a" 1 "b" 3 "c" 2
Tsort = sortrows(T,'val')
Tsort = 3×2 table
name val ____ ___ "a" 1 "c" 2 "b" 3

카테고리

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