2015-03-02 165 views
1

我有一个单元阵列是三维的,即16x10x3,我希望对于所有的值在:,:,1:,:,2:,:,3要被提取到每个矩阵的一列。从3D单元阵列为二维矩阵中提取数据 - MATLAB

我想到了预分配矩阵,然后只是运行一个基本的循环如:

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

是否有这样做,而不必依赖于一个循环的更有效的方法?

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

enter image description here

+1

你是什么意思单?你的意思是你在每个单元中有不同数量的元素?或者你的意思是单一的,如同浮动?如果在每个单元中没有相同数量的元素。是否有可能保证矩阵具有正确的维度? – patrik 2015-03-02 12:59:57

+0

我已经使用部分单元格阵列的示例更新了OP。这些值都是双倍的,但是每个第三维的条目数量不同。例如:,:,1可以包含总共150个条目,而:,:,2只能包含90个。 – AnnaSchumann 2015-03-02 14:41:05

+0

我不确定我们是否可以保证矩阵具有正确的维度。 – AnnaSchumann 2015-03-02 14:44:21

回答

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{:}] 
+0

昨天给我看看之后,我应该考虑使用bsxfun!谢谢! – AnnaSchumann 2015-03-02 17:14:35

+0

@AnnaSchumann是的!这是一个简单的扩展! :) – Divakar 2015-03-02 17:15:10