extract elements from four matrices and create new matrix
Mostrar comentarios más antiguos
I have four matrices of 4*4 order.
For example, the matrices are like [1 2 3 4; 2 3 4 1; 2 2 1 1; 1 2 1 0]
I want to extract the first element of all the four matrices and place it a 2*2 order matrix
repeat for each element and get a 2*2 matrix. how to store all such matrices of order 2*2 in a single 3D matrix?
can someone help me in writing the code?
2 comentarios
Bob Thompson
el 29 de En. de 2019
I don't use it often enough to know the exact command, but I'm fairly sure you can do this with reshape().
James Tursa
el 29 de En. de 2019
What would the dimensions be of the result?
Respuestas (2)
Andrei Bobrov
el 29 de En. de 2019
Editada: Andrei Bobrov
el 29 de En. de 2019
Let A,B,C,D - your matrix (4 x 4).
out = reshepe(permute(cat(3,A,B,C,D),[3,1,2]),2,2,[]);
or
out = reshepe(permute(cat(3,A,B,C,D),[3,2,1]),2,2,[]);
If your 4 4x4 matrices are not already stored as 4x4x4 3D array, well why not? and make them so:
matrices = cat(3, m1, m2, m3, m4); %or if they are in a cell array cat(3, yourcellarray{:})
then it's not clear which order you want the elements to go in the 2x2 matrix, either
[1 3
2 4]
or
[1 3
2 4]
Either way, it's trivial to get your 2x2x16 array:
result = reshape(permute(matrices, [3 1 2]), [2 2 16])
replace [3 2 1] by [3 1 2] if you want the other option.
Categorías
Más información sobre Resizing and Reshaping Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!