user3449015
user3449015

Reputation: 3

convert several 2 dimensional data into 3 dimensional

I would like to combine several 2-D data into 3 dimensional data. I have 124 2-D data (151 x 151) now I want to combine all these data into 3 dimensional, so that it will be like this 124 x 151 x 151. The 2-D data contains NaN elements.

Upvotes: 0

Views: 412

Answers (1)

chappjc
chappjc

Reputation: 30589

To concatenate multiple 2D arrays into a 3D array, use the generalized concatenation function cat, specifying dimension 3.

For example, given 2D arrays A1,A2,... of equal size:

M = cat(3,A1,A2,...)

Say you have k 2D arrays organized in a cell array, C, where each cell is a 2D matrix, all of size M-by-N:

M = cat(3,C{:});

Then M will be of size M-by-N-by-k. Now if you want to go from M-by-N-by-k, to k-by-M-by-N, use permute or shiftdim:

Mn = permute(M,[3 1 2]); % my preference
Mn = shiftdim(M,2);

NOTE: An alternative to cat is cell2mat:

M = cell2mat(reshape(C,1,1,[]))

Upvotes: 1

Related Questions