How Change Matrix Shape / Setting

2 Ansichten (letzte 30 Tage)
Juan Pablo
Juan Pablo am 3 Dez. 2019
Kommentiert: Juan Pablo am 4 Dez. 2019
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.

Akzeptierte Antwort

David Hill
David Hill am 3 Dez. 2019
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
  1 Kommentar
Juan Pablo
Juan Pablo am 4 Dez. 2019
Thank for your help David!. It was very helpful!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by