Sort columns of a table by the value corresponding

31 ビュー (過去 30 日間)
Turner
Turner 2023 年 2 月 19 日
コメント済み: Ron 2024 年 6 月 29 日
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 件のコメント
Turner
Turner 2023 年 2 月 20 日
Row 1 sorted in descending order. I'm able to do that if I just take row 1 and make it an array and use sort(), but then the names of each column are no longer associated with the correct numerical value, so I can't label a figure correctly. So basically I'm trying to get the row sorted in decending order while its still a table so each value is aligned with its correct variable name.
An open ended project, but yes hw essentially.
Stephen23
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 = 5×1 table
V ______ H 0.141 He 0 Li 0.0146 Be 0.0045 B 0.0567
T = sortrows(T,'V')
T = 5×1 table
V ______ He 0 Be 0.0045 Li 0.0146 B 0.0567 H 0.141
Better data design makes code simpler, more robust, and more efficient.

サインインしてコメントする。

採用された回答

Walter Roberson
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 件のコメント
Turner
Turner 2023 年 2 月 20 日
Which representation would be the best to change it to in order to keep the variable names associated with each value? As that is what I'm attempting
Walter Roberson
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
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').'))
T = 1×10 table
A B C D E F G H I J _______ ______ _______ _______ _______ _______ ______ _______ ______ _______ 0.87544 0.0112 0.93254 0.65738 0.71669 0.62776 0.3965 0.90837 0.8422 0.10215
% sort the values, descending
[~,idx] = sort(T{:,:},'descend');
% reorder the table columns
T = T(:,idx)
T = 1×10 table
C H A I E D F G J B _______ _______ _______ ______ _______ _______ _______ ______ _______ ______ 0.93254 0.90837 0.87544 0.8422 0.71669 0.65738 0.62776 0.3965 0.10215 0.0112
  4 件のコメント
Walter Roberson
Walter Roberson 2024 年 6 月 28 日
sortrows(Data, 9, 'descending')
Ron
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 ExchangeShifting and Sorting Matrices についてさらに検索

タグ

製品


リリース

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by