extract elements from four matrices and create new matrix

3 views (last 30 days)
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 Comments
Bob Thompson
Bob Thompson on 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().

Sign in to comment.

Answers (2)

Andrei Bobrov
Andrei Bobrov on 29 Jan 2019
Edited: Andrei Bobrov on 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,[]);

Guillaume
Guillaume on 29 Jan 2019
Edited: Guillaume on 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.

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!