重塑细胞:将具有 i 个观察值的 1xj 细胞转换为具有 j 个观察值的 1xi 细胞

Reshaping cells: transform a 1xj cell with i observations into a 1xi cell with j observations

我想知道重塑细胞的最佳实践。

假设我有一个 1x5 单元格,每个单元格中有 2 个观测值。在我的示例中,我将使用此单元格 VAR。以运行为例:

cbar=linspace(0,1,2);
for i=1:5
    for j=1:2
VAR{i}(j)=i+cbar(j);
    end
end

假设我想创建另一个 1x2 的单元格,每个单元格将有 5 个观察值 - 所以我正在重塑上面的 VAR 单元格。我处理这个问题的方法如下

for i=1:5
    for j=1:2
   VAR_new{j}(i)=VAR{i}(j);
    end
end

碰巧由于某种原因 VAR_new 是空的。

我怎样才能正确地做到这一点?谢谢!

您可以组合 reshape to reshape the matrix to the desired dimensions, and mat2cell 将矩阵转换为元胞数组:

VAR_new = mat2cell(reshape([VAR{:}],5,[]),5,repelem(1,2));

>> VAR_new

VAR_new =

  1×2 cell array

    {5×1 double}    {5×1 double}

>> VAR_new{1}

     1
     2
     2
     3
     3

>> VAR_new{2}

     4
     4
     5
     5
     6