Create sub-matrix respecting an order
10 views (last 30 days)
Show older comments
I have a matrix size 4x4, I want to compose this matrix to submatrix as showing in the picture.
A = [1 7 9 8; 4 16 12 3; 5 22 14 8; 1 10 27 2]
i would like to create automatically 4 matrix 2x2 as showing in the picture and to return their values
I really appreciate any help
0 Comments
Accepted Answer
Jan
on 13 Mar 2021
Edited: Jan
on 13 Mar 2021
A = [1 7 9 8; 4 16 12 3; 5 22 14 8; 1 10 27 2]
C = mat2cell(A, [2,2], [2,2])
Now the 4 matrices are C{1} to C{4}, or if you want C{1,1}, C{1,2}, C{2,1}, C{2,2}.
If you think of creating 4 different matrices A1, A2, A3, A4, this is most likely a bad idea. Hiding an index in the name of the variable is not efficient. But possible:
A1 = A(1:2, 1:2); % Ugly, don't do this except you are really sure
A2 = A(3:4, 1:2);
A2 = A(1:2, 3:4);
A3 = A(3:4, 3:4);
For hiding indices in names see: FAQ: How can I create variables A1, A2,...,A10 in a loop?
4 Comments
Image Analyst
on 14 Mar 2021
If you have only a few (2 to 9) tiles that you want to split your image up into, then I don't have a problem with separate variables, but if you have dozens, you should really use mat2cell(). On the other hand, it depends on what you're going to do with them once each tile has been extracted. There's a chance you should be using blockproc(). This will scan the image and extract the tile automatically and apply some function you'd like to run on each tile, then return the entire processed image with all tiles stitched back together again into a single image. Let me know if you want blockproc demos.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!