extract elements from four matrices and create new matrix
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
Bob Thompson
2019-1-29
I don't use it often enough to know the exact command, but I'm fairly sure you can do this with reshape().
回答(2 个)
Andrei Bobrov
2019-1-29
编辑:Andrei Bobrov
2019-1-29
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 个评论
Guillaume
2019-1-29
编辑:Guillaume
2019-1-29
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 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!