在 Matlab 中取消堆叠多列

Unstacking multiple columns in Matlab

如何在 matlab 中取消堆叠数据? tables 的 unstack 只允许我对一列执行此操作,并且似乎无法执行 Python.

中可用的多索引解决方案

我希望转换为:

进入这个:

在 Matlab 中。

这是生成初始 table T:

的代码
Col1 = [1,2,4,1,3,1,2,3,4].';
Col2 = {'A','A','A','B','B','C','C','C','C'}.';
Col3 = [0.48,0.78,0.58,0.01,0.53,0.26,0.93,0.69,0.45].';
Col4 = [0.95,0.29,0.90,0.72,0.07,0.23,0.81,0.22,0.88].';

T = table( Col1, Col2, Col3, Col4 );

您可以使用 unstack 函数通过为第二个输入指定多个变量来做到这一点

Col1 = [1,2,4,1,3,1,2,3,4].';
Col2 = {'A','A','A','B','B','C','C','C','C'}.';
Col3 = [0.48,0.78,0.58,0.01,0.53,0.26,0.93,0.69,0.45].';
Col4 = [0.95,0.29,0.90,0.72,0.07,0.23,0.81,0.22,0.88].';

T = table( Col1, Col2, Col3, Col4 );

tUnstack = unstack( T, {'Col3','Col4'}, 'Col2'  );

输出:

请注意,在 MATLAB 中,table 中不能有多个 header 行。 unstack 函数连接多个类别的 header 个名称(如上所示),或者如果您希望指定它们,可以采用额外的 'NewDataVariableNames' 参数。

如果您需要多个 header 行,则必须解析出 header 并将新数据存储在元胞数组中。