Sort columns of a table by the value corresponding
조회 수: 31 (최근 30일)
이전 댓글 표시
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
Stephen23
2023년 2월 20일
편집: Stephen23
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.
채택된 답변
Walter Roberson
2023년 2월 20일
[~, idx] = sort(table2array(YourTable), 2, 'descend');
out = YourTable(:, idx) ;
Note that this implementation will only work for a single row, as it rearranges the table object. If you needed multiple rows then the variable names would have to be mixed with the numeric values, which is possible with a change of representation.
댓글 수: 2
Walter Roberson
2023년 2월 20일
My proposal is the same as what Voss suggested, just that I tend to use different helper functions, such as
[~,idx] = sort(table2array(T),'descend');
The crucial step is taking the sort order information from sort() and using it to index the table variables, which re-arranges the table to have the variables in that order.
The "different representation" I was referring to would apply to the case where you had multiple rows in the table and wanted to process each row individually: in such a case you would not be able to have two different variable orders in two different rows of the same table() object. In such a situation you could use representations such as
'Li', 0.146, 'H', 0.141, 'He', 0
'H', 0.314, 'Li', 0.11', 'C', 0.04
right inside the table, with odd-numbered variables being element abbreviations and the even-numbered variables being the coefficient that went with it.
Or you could use the data to construct a table such as
'Li: 0.146', 'H: 0.141', 'He: 0'
'H: 0.314', 'Li: 0.11', 'C: 0.04'
with text for all entries. Or you might prefer text but
'0.146: Li', '0.141: H', '0: He'
'0.314: H', '0.11: Li', '0.04: C'
The method that I had suggested, and which Voss showed explicit code for, works fine for a single row; you only need to care about alternate forms if you have multiple rows.
추가 답변 (1개)
Voss
2023년 2월 20일
% 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
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.
참고 항목
카테고리
Help Center 및 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!