Table: Reshape based on Cell Column and Sort

조회 수: 1 (최근 30일)
Dave
Dave 2019년 1월 12일
댓글: Dave 2019년 1월 13일
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 ...

채택된 답변

Guillaume
Guillaume 2019년 1월 12일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by