Sort columns of a table by the value corresponding
31 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Turner
am 19 Feb. 2023
Kommentiert: Ron
am 29 Jun. 2024
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 Kommentare
Stephen23
am 20 Feb. 2023
Bearbeitet: Stephen23
am 20 Feb. 2023
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.
Akzeptierte Antwort
Walter Roberson
am 20 Feb. 2023
[~, 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 Kommentare
Walter Roberson
am 20 Feb. 2023
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.
Weitere Antworten (1)
Voss
am 20 Feb. 2023
% 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 Kommentare
Ron
am 29 Jun. 2024
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.
Siehe auch
Kategorien
Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!