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 CenterFile Exchange에서 Shifting 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