extract elements from four matrices and create new matrix
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Bob Thompson
am 29 Jan. 2019
I don't use it often enough to know the exact command, but I'm fairly sure you can do this with reshape().
Antworten (2)
Andrei Bobrov
am 29 Jan. 2019
Bearbeitet: Andrei Bobrov
am 29 Jan. 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,[]);
0 Kommentare
Guillaume
am 29 Jan. 2019
Bearbeitet: Guillaume
am 29 Jan. 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.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Resizing and Reshaping 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!