【问题标题】:Extracting data from a 3D cell array into a 2D Matrix - MATLAB将 3D 元胞数组中的数据提取到 2D 矩阵中 - MATLAB
【发布时间】:2015-03-02 12:24:48
【问题描述】:

我有一个三维元胞数组,即 16x10x3,我希望将 :,:,1 :,:,2:,:,3 中的所有值分别提取到矩阵的一列中。

我考虑过预先分配一个矩阵,然后只运行一个基本循环,例如:

for m=1:3
mat(:,m) = ([a{:,:,m}]); 
end

有没有更有效的方法来做到这一点而不必依赖循环?

编辑::,:,1/2/3 之间有不同数量的值。

【问题讨论】:

  • 单身是什么意思?你的意思是每个单元格中有不同数量的元素?或者你的意思是像浮动一样单?如果每个单元格中的元素数量不同。是否可以保证矩阵具有正确的维度?
  • 我已经用单元格数组的一部分示例更新了 OP。这些值都是双精度值,但是每个第三维都有不同数量的条目。例如:,:,1总共可以包含150个条目,而:,:,2只能包含90个。
  • 我不确定您的意思是我们是否可以保证矩阵具有正确的尺寸。
  • 并用零填充“空”空格?

标签: arrays matlab matrix cell-array


【解决方案1】:

是时候进入bsxfun了!这是实现 -

%// Get the number of elements in each column of the input cell array
lens = sum(cellfun('length',reshape(a,[],size(a,3))),1)

%// Store the maximum number of elements possible in any column of output array
max_lens = max(lens)  

%// Setup output array, with no. of rows as max number of elements in each column
%// and no. of columns would be same as the no. of columns in input cell array 
mat = zeros(max_lens,numel(lens))

%// Create as mask that has ones to the "extent" of number of elements in
%// each column of the input cell array using the lengths
mask = bsxfun(@le,[1:max_lens]',lens)  %//'

%// Finally, store the values from input cell array into masked positions
mat(mask) = [a{:}]

【讨论】:

  • 在你昨天给我看了bsxfun之后,我应该想到使用它!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-23
  • 2015-02-04
  • 1970-01-01
  • 1970-01-01
  • 2015-03-30
  • 1970-01-01
相关资源
最近更新 更多