converting 3d matrix into a cell array

조회 수: 11 (최근 30일)
AT_HYZ
AT_HYZ 2024년 3월 21일
답변: Dyuman Joshi 2024년 3월 22일
Hi,
I have a 3D matrix (1001x 259x 259) and want to convert to a cell array (1x1001) in which each cell array has 259x259 matrix (2D matrix). Could anyone help?
Thanks.
  댓글 수: 1
AT_HYZ
AT_HYZ 2024년 3월 22일
I tried the below code from this forum
https://www.mathworks.com/matlabcentral/answers/344044-transform-a-3d-matrix-into-cell-array
Mycell = num2cell(Mymatrix,[2 3]);
But I want the 2D matrix inside each cell array.

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

채택된 답변

Dyuman Joshi
Dyuman Joshi 2024년 3월 22일
@AT_HYZ, change the dimensions and use the above transformation -
abc = rand(1001,259,259);
%Shift dimensions
Mymatrix = shiftdim(abc, 1);
%Check size
size(Mymatrix)
ans = 1x3
259 259 1001
%Use num2cell
Mycell = num2cell(Mymatrix,[1 2]);
size(Mycell)
ans = 1x3
1 1 1001
%Change the size of the cell array as required
Mycell = reshape(Mycell, 1, 1001);
%Check the size of data inside a cell element
size(Mycell{1})
ans = 1x2
259 259

추가 답변 (1개)

James Tursa
James Tursa 2024년 3월 22일
편집: James Tursa 2024년 3월 22일
x = rand(1001,259,259);
result = arrayfun(@(k)squeeze(x(k,:,:)),1:size(x,1),'uni',false);
size(result)
ans = 1x2
1 1001
size(result{1})
ans = 1x2
259 259
But, be advised that for matrix manipulation it might be better to keep this data as a 3D array with dimensions 259x259x1001 (i.e., permute(x,[2 3 1])) so that you can take advantage of the page functions like pagemtimes, pagemldivide, etc.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by