extract elements from four matrices and create new matrix

5 visualizzazioni (ultimi 30 giorni)
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 Commenti
Bob Thompson
Bob Thompson il 29 Gen 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
James Tursa il 29 Gen 2019
What would the dimensions be of the result?

Accedi per commentare.

Risposte (2)

Andrei Bobrov
Andrei Bobrov il 29 Gen 2019
Modificato: Andrei Bobrov il 29 Gen 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,[]);

Guillaume
Guillaume il 29 Gen 2019
Modificato: Guillaume il 29 Gen 2019
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.

Categorie

Scopri di più su Resizing and Reshaping Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by