How to convert from cell array to multidimensional array?

조회 수: 22 (최근 30일)
Anton Filyayev
Anton Filyayev 2015년 11월 24일
편집: Christopher Crawford 2020년 1월 12일
I have a cell array that is 3x3 with each cell containing a vector of (1x1024). It is essentially the same as a 3x3 matrix with a dimension of 1024. How can I convert from cell array to multidimensional array that has these dimensions of (3,3,1024)? I tried cell2mat, but it simply concatenates them into a 3x3072 matrix.

채택된 답변

Andrei Bobrov
Andrei Bobrov 2015년 11월 24일
A -your cell array [3 x 3].
Z = cellfun(@(x)reshape(x,1,1,[]),A,'un',0);
out = cell2mat(Z);
  댓글 수: 2
Anton Filyayev
Anton Filyayev 2015년 11월 24일
편집: Anton Filyayev 2015년 11월 24일
This worked beautifully, thanks!!
Christopher Crawford
Christopher Crawford 2020년 1월 12일
편집: Christopher Crawford 2020년 1월 12일
To do the same for a cell array A of arbitrary ndarrays, replace [] with size(x):
B=cell2mat(cellfun(@(x)reshape(x,[1,1,size(x)]),A,'un',0))
which creates an array 2 dimensions higer than that of the cells of A.
To only add one dimension for row or collumn cell arrays (and none for a singleton cell array):
C=reshape(B,[size(A)(size(A)~=1),size(A{1})])
or permute the ndarray dimensions ahead of the cell array dimensions:
C=permute(B,[3:ndims(B),1:2])

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by