cell array into 3d matrix

10 ビュー (過去 30 日間)
sumeeth
sumeeth 2012 年 4 月 17 日
編集済み: Andrei Bobrov 2013 年 11 月 17 日
i have a cell array of size 104x104 with each cell having a matrix of size 1x1000. How can i convert this cell into a 3d matrix of size 104x104x1000 ??
  1 件のコメント
Sean de Wolski
Sean de Wolski 2012 年 4 月 17 日
+1, good clear question with room for many good approaches

サインインしてコメントする。

回答 (4 件)

Hanan Shteingart
Hanan Shteingart 2013 年 11 月 17 日
You can concatenate cell array content using the "cat" function with this syntax: cat(dim, A{:}) where A is your cell array

the cyclist
the cyclist 2012 年 4 月 17 日
Both of these should do the same thing (and I think it is the thing you want). The second is a little bit more transparent, but slower.
B1 = cell2mat(arrayfun(@(x)permute(x{:},[3 1 2]),A,'UniformOutput',false));
B2 = zeros(104,104,1000);
for i = 1:104,
for j = 1:104,
B2(i,j,1:1000) = permute(A{i,j},[1 3 2]);
end
end
  1 件のコメント
Sean de Wolski
Sean de Wolski 2012 年 4 月 17 日
Reshape() instead of permute() for speed.

サインインしてコメントする。


Sean de Wolski
Sean de Wolski 2012 年 4 月 17 日
Or the pure matrix manipulation approach:
%Sample Data:
X = reshape(magic(50),1,[]);
C = cell(100,100);
C(:) = {X}; %100 x 100 cell array of xs
%Engine:
D = reshape(reshape(cell2mat(C)',numel(X),numel(C))',size(C,1),size(C,2),numel(X)); %some reshapin'
%Check
all(all(all(bsxfun(@eq,D(1,1,:),D)))) %Is it right?
yay!

Andrei Bobrov
Andrei Bobrov 2012 年 4 月 17 日
編集済み: Andrei Bobrov 2013 年 11 月 17 日
permute(reshape(cell2mat(C).',numel(C{1}),size(C,2),[]),[3 2 1])
or
reshape(cat(1,A{:}),[size(C), numel(C{1})])

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by