How Change Matrix Shape / Setting

2 次查看(过去 30 天)
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.

采纳的回答

David Hill
David Hill 2019-12-3
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

更多回答(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