Table: Reshape based on Cell Column and Sort

4 Ansichten (letzte 30 Tage)
Dave
Dave am 12 Jan. 2019
Kommentiert: Dave am 13 Jan. 2019
Is there a way to reshape or transform a table 7x3 table into a table with as many columns as the unique elements of a cell column?
Orignal table 7x3 (var1 double, var2 cell, var3 double)
Table1=
1 AAA 100
3 AAA 500
1 BBB 300
2 AAA 250
4 BBB 50
2 BBB NaN
3 BBB 100
A sub table,3x3, for only AAA would be
Table2=
1 AAA 100
2 AAA 250
3 AAA 500
A subtable, 4x3, for only BBB would be
Table3=
1 BBB 300
2 BBB NaN
3 BBB 100
4 BBB 50
The target table, 4x3, is
Table4=
1 100 300
2 250 NaN
3 500 100
4 NaN 50
I do not need table 2 and 3, only interested in table 4. Table 4 keeps the first column and following columns refer to the AAA BBB ...

Akzeptierte Antwort

Guillaume
Guillaume am 12 Jan. 2019
unstack will do exactly that:
>> t = table([1;3;1;2;4;2;3], {'AAA';'AAA';'BBB';'AAA';'BBB';'BBB';'BBB'},[100;500;300;250;50;NaN;100])
t =
7×3 table
Var1 Var2 Var3
____ _____ ____
1 'AAA' 100
3 'AAA' 500
1 'BBB' 300
2 'AAA' 250
4 'BBB' 50
2 'BBB' NaN
3 'BBB' 100
>> unstack(t, 'Var3', 'Var2')
ans =
4×3 table
Var1 AAA BBB
____ ___ ___
1 100 300
3 500 100
2 250 NaN
4 NaN 50

Weitere Antworten (0)

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!

Translated by