Borrar filtros
Borrar filtros

How Change Matrix Shape / Setting

3 visualizaciones (últimos 30 días)
Juan Pablo
Juan Pablo el 3 de Dic. de 2019
Comentada: Juan Pablo el 4 de Dic. de 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.

Respuesta aceptada

David Hill
David Hill el 3 de Dic. de 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 comentario
Juan Pablo
Juan Pablo el 4 de Dic. de 2019
Thank for your help David!. It was very helpful!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by