필터 지우기
필터 지우기

Create sub-matrix respecting an order

조회 수: 6 (최근 30일)
Adrian Brown
Adrian Brown 2021년 3월 13일
댓글: Adrian Brown 2021년 3월 14일
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

채택된 답변

Jan
Jan 2021년 3월 13일
편집: Jan 2021년 3월 13일
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
Image Analyst
Image Analyst 2021년 3월 14일
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 2021년 3월 14일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by