Create sub-matrix respecting an order

3 views (last 30 days)
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

Accepted Answer

Jan
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);
  4 Comments
Image Analyst
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.
Adrian Brown
Adrian Brown on 14 Mar 2021
Dear @Image Analyst thank you so much for your reply. actually my main matrix can change and it can be for dozens. my purpose is to extract the submatrix and to do some calculation on each submatrix as calculating the average of such submatrix, after calculating the average of each submatrix I am planning to choose a specipfic submatrix according to the result of average (use the submatrix that have the highest average). next I need to build the main matrix using cell2mat() function from the submatrices again.
I am wondering if you could please help with any code or suggest that I will be very greatfull to you.
Blockpro demo will be so helpfull.
Thank you so much for your help and support.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!