MATLAB: How Change Matrix Shape / Setting

arrayconcatenatematrixreshape

I should arrange the matrix [Nx, 2, Nz] in a matrix [(Nx*Nz)/4, 8], where Nx and Nz are always even numbers,
I was trying to get this new array with this matrix [16 x 2 x 6] and get the final matrix [24 x 8],
M1.1 (2).png
and using the following code, where X = [16 x 2 x 6]
First I create a only array with concatenate function:
A = [];
for n = 1:size(X,3)
A = cat(1,A,X(:,:,n));
end
And get the matrix A = [96 x 2], and for getting the final matrix, I was using this code line:
B=cell2mat(mat2cell(reshape(A',4,[]),4,repmat(numel(A)/8,1,2))')';
As it is explained in the graph, I want to join the first 2 columns and 2 rows of Nz(1) (4 elements) and Nz(2) (4 elements) in only one row with 8 columns.
The line code works with some arrays but not with all of them.
If someone of you can help with this, I would appreciate your contribution.

Best Answer

This might not be the best way, but it works.
C=[];
c=1;
for k=2:2:size(val,3)
B(:,:,c)=[val(:,:,k-1),val(:,:,k)];%horizontal cat
c=c+1;
end
for k=1:size(val,3)/2
C=[C;B(:,:,k)];%vertical cat
end
C=C';
C=reshape(C,8,[])';%reshape to your desire