MATLAB: Concatenating elements of an array in a certain manner

for loopMATLABmatrix array

Hello all,
I have a matrix b, with the dimension of NxLxNm. I would like to first consider b(:,:,1) up to b(:,:,j-1) where j ranges from 1 to Nm. In each iteration of j, I would like to create a new cell c with a total dimension of 1XL, an element dimension of Nxj-1, which concatenates the elements of b(:,:,1) to b(:,:,j-1) in a way such that e.g. c{1}=[b(1,1,1), b(1,1,2), … b(1,1,j-1); b(2,1,1), b(2,1,2),…b(2,1,j-1),….]; c{2}==[b(1,2,1), b(1,2,2), … b(1,2,j-1); b(2,1,1), b(2,1,2),…b(2,1,j-1),….], and so forth. I would appreciate if somebody can help me please. Thanks.

Best Answer

c=permute(b(:,:,1:j-1),[1,3,2]);
c=num2cell(c,[1,2]);
c=c(:).';