【问题标题】:How to combine row and Column with different size in a cell array into matrix in MATLAB如何将单元格数组中不同大小的行和列组合成MATLAB中的矩阵
【发布时间】:2016-04-06 08:06:41
【问题描述】:

如何有效地将不同大小的单元格array v rowColumn组合成matrix,用0填充向量? 例如,如果我有

A= {[1;2;3]  [1 2 ; 1 3; 2 3]  [1 2 3]};

我想要:

A=[1 0 0
   2 0 0
   3 0 0
   1 2 0
   1 3 0
   2 3 0
   1 2 3]

【问题讨论】:

  • 但是你的回答成功了

标签: matlab matrix cell-array


【解决方案1】:

您可以简单地使用padarrayvertcat 之前用零填充您的数组:

  B = padarray(A{1},[0 3-size(A{1},2)],'post')
  C = padarray(A{2},[0 3-size(A{2},2)],'post')
  D = padarray(A{3},[0 3-size(A{3},2)],'post')
  %//Note the 3-size(A{1},2)... The 3 comes from the number of columns you want your final matrix to be, and it cannot be smaller than the maximum value of size(A{N},2) in your case it is 3, since A{3} is 3 columns wide. 
  result = vertcat (B,C,D)

  result =

     1     0     0
     2     0     0
     3     0     0
     1     2     0
     1     3     0
     2     3     0
     1     2     3

您可以编写一个循环来遍历您的单元格或使用 cellfun 进行并行化。

在一个简单的循环中,它看起来像:

 result = [];
 for t = 1:size(A,2)
    B = padarray(A{t},[0 3-size(A{t},2)],'post');
    result = vertcat(result,B);
 end

【讨论】:

  • 抱歉,当我将元胞数组更改为更大的元胞数组时,matlab 出现错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-06-14
  • 1970-01-01
  • 1970-01-01
  • 2021-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多