Sub-matrix Access in a Square Pattern
이전 댓글 표시
I need a method of accessing an array in a certain pattern that I will describe here with an example:
A = [1,2,3,4;5,6,7,8;9,10,11,12;13,14,15,16];
I need some function or method to access this array in a pattern as such:
A1 = [1,2;5,6]
A2 = [3,4;7,8]
A3 = [9,10;13,14]
A4 = [11,12;15,16]
Attached is an image showing it for an 8x8 matrix
Done in that order in that pattern, hence the title "Square" Pattern. If anyone could detail a way to do so that would be much appreciated, thanks.
채택된 답변
추가 답변 (2개)
Rik
2020년 9월 22일
2 개 추천
It looks like either blockproc or mat2cell is what you're looking for.
댓글 수: 1
Dylan Tarter
2020년 9월 22일
편집: Dylan Tarter
2020년 9월 22일
Using mat2tiles from
submatrices=mat2tiles(A,[2,2]).'
댓글 수: 6
Dylan Tarter
2020년 9월 22일
ive tried this mat2tiles and it doesnt let me do something to each tile individually
Certainly it does. Want to find the diagonal of each submatrix for example? Then,
>> D=cellfun(@diag, submatrices,'uni',0); D{:}
ans =
1
6
ans =
3
8
ans =
9
14
ans =
11
16
Dylan Tarter
2020년 9월 22일
Matt J
2020년 9월 22일
The pattern that that outputs is:
No, that is incorrect. The ordering is what you specified in your post. Note the transpose.
>> submatrices=mat2tiles(A,[2,2]).'; submatrices{:}
ans =
1 2
5 6
ans =
3 4
7 8
ans =
9 10
13 14
ans =
11 12
15 16
Dylan Tarter
2020년 9월 22일
편집: Dylan Tarter
2020년 9월 22일
>> submatrices=mat2tiles( mat2tiles(A,[2,2]),[2,2]).';
>> U=cellfun(@(c) cell2mat(reshape(c.',[],1)),submatrices,'uni',0);
>> A,horzcat(U{:})
A =
1 9 17 25 33 41 49 57
2 10 18 26 34 42 50 58
3 11 19 27 35 43 51 59
4 12 20 28 36 44 52 60
5 13 21 29 37 45 53 61
6 14 22 30 38 46 54 62
7 15 23 31 39 47 55 63
8 16 24 32 40 48 56 64
U =
1 9 33 41 5 13 37 45
2 10 34 42 6 14 38 46
17 25 49 57 21 29 53 61
18 26 50 58 22 30 54 62
3 11 35 43 7 15 39 47
4 12 36 44 8 16 40 48
19 27 51 59 23 31 55 63
20 28 52 60 24 32 56 64
>> U=mat2tiles(U,[2,2]); U{:}
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!