Sort columns of a table by the value corresponding
이전 댓글 표시
Ive attached a sc of my table, although it continues to 86 columns, I would like to sort the values corresponding to each variable in decending order, and have the variable names be sorted along with the value.
The table dimensions is 1x86 as seen in the screenshot

댓글 수: 3
the cyclist
2023년 2월 20일
I don't really follow what you want. Do you want each column sorted independently from the others?
And then you want the columns sorted alphabetically according to the column names (e.g. Ac first, Zr last)?
Out of curiosity ... why? Guessing homework.
Turner
2023년 2월 20일
If the table was arranged transposed, then this would be trivial using SORTROWS():
V = [0.141;0;0.0146;0.0045;0.0567];
T = table(V,'RowNames',{'H','He','Li','Be','B'})
T = sortrows(T,'V')
Better data design makes code simpler, more robust, and more efficient.
채택된 답변
추가 답변 (1개)
% I make a table of random data with 10 columns and 1 row, to represent your table
C = num2cell(rand(1,10));
T = table(C{:},'VariableNames',cellstr(('A':'J').'))
% sort the values, descending
[~,idx] = sort(T{:,:},'descend');
% reorder the table columns
T = T(:,idx)
댓글 수: 4
Turner
2023년 2월 20일
Ron
2024년 6월 28일
I have this data. the last row (the one highlighted in blue) is the sum of all the values in each column. I want to re-arrange all the columns as per the descending values in this last row but I am unable to do so. I tried your code and it did work for all other tables but I dont know why its not working for my data. Can you please help?

Walter Roberson
2024년 6월 28일
sortrows(Data, 9, 'descending')
Ron
2024년 6월 29일
Thankyou so much for taking out time and replying to my question. Although I was able to find out that one of the elements was a string instead of numeric and thats why I was not able to sort to but thankyou once again for helping.
카테고리
도움말 센터 및 File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!