Create sub-matrix respecting an order

2 views (last 30 days)
Adrian Brown
Adrian Brown on 13 Mar 2021
Commented: Adrian Brown on 14 Mar 2021
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 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);
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!