MATLAB: 3D Matrix to 2D Matrix

3d to 2d conversionfor loopmatrixmatrix arraymatrix manipulation

I have four 3D matrix like Y= ones(2,2,4) and i want to make a new matrix like YT=zeros(8,8).
I want to take values from 3D "Y" matrix and insert them into 2D "YT" matrix in a following fashion
YT(1,1)=Y(1,1,1)
YT(1,2)=Y(1,2,1)
YT(2,1)=Y(2,1,1)
YT(2,2)=Y(2,2,1)
YT(3,3)=Y(1,1,2)
YT(3,4)=Y(1,2,2)
YT(4,3)=Y(2,1,2)
YT(4,4)=Y(2,2,2)
YT(5,5)=Y(1,1,3)
YT(5,6)=Y(1,2,3)
YT(6,5)=Y(2,1,3)
YT(6,6)=Y(2,2,3)
YT(7,7)=Y(1,1,4)
YT(7,8)=Y(1,2,4)
YT(8,7)=Y(2,1,4)
YT(8,8)=Y(2,2,4)
Can some one suggest me what would be the best way to do this thing automatically? I only assume the values of Y to 1,1,1,1 but in my real case, values of Y are different complex numbers.

Best Answer

This works:
y = 1:16; % Create Data

Y = reshape(y, 2, 2, 4); % Create Data
RY = reshape(Y(:), 2, []); % Reshape ‘Y’
RC = mat2cell(RY, 2, ones(1,size(RY,2)/2)*2); % Create Submatrices
YT = blkdiag(RC{:}) % Create ‘YT’