How to change table dimensions by ordering by column values?

Here is my table so far:
1 -1 0.532
1 -2 0.765
1 0 0.726
1 1 0.526
1 2 0.915
2 -1 0.693
2 -2 0.485
2 0 0.624
2 1 0.627
2 2 1.197
3 -1 0.647
3 -2 0.850
3 0 0.723
3 1 0.516
3 2 0.706
How can I change it into a format that looks like this:
-1 -2 0 1 2
1
2
3

 Réponse acceptée

Assume you put in those 15 numbers into a column vector:
a=(1:15)';
b=reshape(a,5,3);
t=array2table(b');
t.Properties.VariableNames={'N1','N2','Zero','One','Two'};
t.Properties.RowNames={'One','Two','Three'}
t =
3×5 table
N1 N2 Zero One Two
__ __ ____ ___ ___
One 1 2 3 4 5
Two 6 7 8 9 10
Three 11 12 13 14 15

Plus de réponses (1)

Not clear how you need to use your result, but this is exactly what unstack does:
t =
15×3 table
Var1 Var2 Var3
____ ____ _____
1 -1 0.532
1 -2 0.765
1 0 0.726
1 1 0.526
1 2 0.915
2 -1 0.693
2 -2 0.485
2 0 0.624
2 1 0.627
2 2 1.197
3 -1 0.647
3 -2 0.85
3 0 0.723
3 1 0.516
3 2 0.706
>> t2 = unstack(t,'Var3','Var2','GroupingVariable','Var1');
Warning: Variable names were modified to make them valid MATLAB identifiers.
>> t2.Var1 = [];
>> t2.Properties.RowNames = {'One' 'Two' 'Three'};
>> t2.Properties.VariableNames = {'MinusTwo' 'MinusOne' 'Zero' 'One' 'Two'}
t2 =
3×5 table
MinusTwo MinusOne Zero One Two
________ ________ _____ _____ _____
One 0.765 0.532 0.726 0.526 0.915
Two 0.485 0.693 0.624 0.627 1.197
Three 0.85 0.647 0.723 0.516 0.706

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by