Create sub-matrix respecting an order
조회 수: 6 (최근 30일)
이전 댓글 표시
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]
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/548428/image.png)
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
채택된 답변
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);
For hiding indices in names see: FAQ: How can I create variables A1, A2,...,A10 in a loop?
댓글 수: 4
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.
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!