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!