Sorting rows only with name of column

1 次查看(过去 30 天)
Hi guys, my question is:
I have a table with 5 rows and 10 columns like this:
Datetime Var1 Var2 Var3 Var4 Var5
________________________________________________________
01-01-90 0.025001 0 0.058841 0 0.059898
01-02-90 0.025318 0 0.068993 0 0.067252
01-03-90 0.025318 0 0.074108 0 0.074613
01-04-90 0.025318 0.019803 0.10536 0 0.10008
01-05-90 0.03774 0.020203 0.1372 0 0.16455
......... etc
My question is: How do I sort each row by column name and get a result like this?
Datetime 1 2 3 4 5
___________________________________
01-01-90 Var5 Var3 Var1 Var2 Var4
01-02-90 Var3 Var5 Var1 Var2 Var4
01-03-90 Var5 Var3 Var1 Var2 Var4
01-04-90 Var3 Var5 Var1 Var2 Var4
01-05-90 Var5 Var3 Var1 Var2 Var4
.......... etc
thanks a lot.

采纳的回答

Image Analyst
Image Analyst 2020-6-12
Try this:
% Setup table.
m = randi(9, 10, 5); % Random integers
dateTimes = repmat(datestr(now), 10, 1)
t1 = table(dateTimes, m(:, 1), m(:, 2), m(:, 3), m(:, 4), m(:, 5), 'VariableNames', {'Datetime', 'Var1', 'Var2', 'Var3', 'Var4', 'Var5'})
% Now we have our input table and we can begin
% Get the variable names from table t1.
t1VariableNames = t1.Properties.VariableNames
% Make an output table called t2.
t2VariableNames = {'Datetime', 'Pos1', 'Pos2', 'Pos3', 'Pos4', 'Pos5'}
t2 = table("Datetime", "Pos1", "Pos2", "Pos3", "Pos4", "Pos5", 'VariableNames', t2VariableNames)
% Now sort each row by columns:
for row = 1 : size(t1, 1)
[~, sortOrder] = sort(t1{row, 2:end}, 'descend');
t2(row, 2:end) = t1VariableNames(sortOrder + 1);
t2(row, 1) = t1(row, 1); % Transfer datetime column.
end
% Display result in command window:
t2
You get:
t2 =
10×6 table
Datetime Pos1 Pos2 Pos3 Pos4 Pos5
______________________ ______ ______ ______ ______ ______
"12-Jun-2020 08:32:30" "Var4" "Var1" "Var2" "Var3" "Var5"
"12-Jun-2020 08:32:30" "Var2" "Var1" "Var3" "Var5" "Var4"
"12-Jun-2020 08:32:30" "Var5" "Var4" "Var1" "Var3" "Var2"
"12-Jun-2020 08:32:30" "Var1" "Var2" "Var4" "Var5" "Var3"
"12-Jun-2020 08:32:30" "Var2" "Var5" "Var3" "Var4" "Var1"
"12-Jun-2020 08:32:30" "Var3" "Var1" "Var2" "Var4" "Var5"
"12-Jun-2020 08:32:30" "Var2" "Var3" "Var1" "Var4" "Var5"
"12-Jun-2020 08:32:30" "Var1" "Var4" "Var2" "Var3" "Var5"
"12-Jun-2020 08:32:30" "Var2" "Var3" "Var1" "Var4" "Var5"
"12-Jun-2020 08:32:30" "Var3" "Var4" "Var1" "Var2" "Var5"
  11 个评论
gcicceri
gcicceri 2020-6-19
Thank you so much! I'll take all your examples as my starting point.
dpb
dpb 2020-6-19
You'll find the time invested will be paid back amply to learn to think of using the vectorized array/matrix expressions and thereby avoid the pitfall of the multiple variables with similar names.
Don't feel badly, though; it's the normal first reaction for everybody when begin--but "the MATLAB way" will soon begin to feel natural and reverting to procedural languages and explicit variables and looping will seem so pedestrian! :)

请先登录,再进行评论。

更多回答(2 个)

dpb
dpb 2020-6-12
编辑:dpb 2020-6-12
ttmp=[table(datetime(string(Datetime),'InputFormat','MM-dd-yy','PivotYear',1900)) array2table(Var)];
>> [~,ix]=sort(ttmp{:,2:end},2,'descend')
ix =
5 3 1 2 4
3 5 1 2 4
5 3 1 2 4
3 5 1 2 4
5 3 1 2 4
>>
If need the text string, use compose.
ADDENDUM: Specifically...
>> tSortV=[ttmp(:,1) cell2table(ttmp.Properties.VariableNames(ix+1), ...
'VariableNames',compose('%d',1:size(tmp,2)))]
tSortV =
5×6 table
DateTime 1 2 3 4 5
___________ ________ ________ ________ ________ ________
01-Jan-1990 {'Var5'} {'Var3'} {'Var1'} {'Var2'} {'Var4'}
02-Jan-1990 {'Var3'} {'Var5'} {'Var1'} {'Var2'} {'Var4'}
03-Jan-1990 {'Var5'} {'Var3'} {'Var1'} {'Var2'} {'Var4'}
04-Jan-1990 {'Var3'} {'Var5'} {'Var1'} {'Var2'} {'Var4'}
05-Jan-1990 {'Var5'} {'Var3'} {'Var1'} {'Var2'} {'Var4'}
>>
or as strings instead of cellstr()
>> tSortV=[ttmp(:,1) array2table(string(ttmp.Properties.VariableNames(ix+1)),'VariableNames',compose('%d',1:size(tmp,2)))]
tSortV =
5×6 table
DateTime 1 2 3 4 5
___________ ______ ______ ______ ______ ______
01-Jan-1990 "Var5" "Var3" "Var1" "Var2" "Var4"
02-Jan-1990 "Var3" "Var5" "Var1" "Var2" "Var4"
03-Jan-1990 "Var5" "Var3" "Var1" "Var2" "Var4"
04-Jan-1990 "Var3" "Var5" "Var1" "Var2" "Var4"
05-Jan-1990 "Var5" "Var3" "Var1" "Var2" "Var4"
K>>
  6 个评论
gcicceri
gcicceri 2020-6-17
编辑:gcicceri 2020-6-17
I have same error....
that is: " '1' is not a valid variable name. ".
Is it possible that problem is relative to my Matlab's version? (I have version 2018a). So, the ‘compose’ function doesn’t works?
Could it be that my variables, starting with an 'x10292', are not recognized as strings?
dpb
dpb 2020-6-17
compose was introduced R2016 but the table variable (and/or row) names being anything other than valid MATLAB variable names wasn't introduced until R2019(not sure of a or b; I saw a note on that once but can't find it at the moment)...
You'll have to have use a variable name beginning with a letter (or upgrade)

请先登录,再进行评论。


Image Analyst
Image Analyst 2020-6-12
Here's one way, perhaps a little easier to understand than dpb's method, though not as compact:
% Setup table.
m = randi(9, 10, 5);
t = array2table(m)
% Now sort each row by columns in descending order:
for row = 1 : size(t, 1)
t{row, :} = sort(t{row, :}, 'descend');
end
% Display result in command window:
t

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by