How to Convert a 3D Matrix into Cell Array of Desire Dimension

5 次查看(过去 30 天)
I have a matrix of N x M x Z 3D matrix. Where N x M is 2 x 100 and Z is 500. I want to convert it into cell array of Z x 1 ( 500 x 1) where each cell element will be N x M (2 x 100). How I can do this?

采纳的回答

Paul
Paul 2023-6-27
A = rand(2,100,500); % example data
C = squeeze(num2cell(A,[1 2])); % create cell array
whos C
Name Size Bytes Class Attributes C 500x1 852000 cell
isequal(cat(3,C{:}),A) % verify
ans = logical
1
  2 个评论
Muhammad
Muhammad 2023-6-27
@Paul what if I have 500 x 2 x 100 matrix to 500x1 cell array of 2x100 in each cell?
Paul
Paul 2023-6-27
A = rand(500,2,100);
C = num2cell(A,[2 3]); % line 1
At this point, each elment of C is 1 x 2 x 100 and is isequal to the corresponding slice of A. For example
size(C{293})
ans = 1×3
1 2 100
isequal(C{293},A(293,:,:))
ans = logical
1
To make each cell 2 x 100
C = cellfun(@(c) squeeze(c),C,'UniformOutput',false); % line 2
size(C)
ans = 1×2
500 1
size(C{293})
ans = 1×2
2 100
isequal(C{293},squeeze(A(293,:,:)))
ans = logical
1
Lines 1 and 2 can be combined into a single line if desired.
There may be a better way to do this.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by