Sub-matrix Access in a Square Pattern
Ältere Kommentare anzeigen
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.
Akzeptierte Antwort
Weitere Antworten (2)
Rik
am 22 Sep. 2020
2 Stimmen
It looks like either blockproc or mat2cell is what you're looking for.
1 Kommentar
Dylan Tarter
am 22 Sep. 2020
Bearbeitet: Dylan Tarter
am 22 Sep. 2020
Using mat2tiles from
submatrices=mat2tiles(A,[2,2]).'
6 Kommentare
Dylan Tarter
am 22 Sep. 2020
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
am 22 Sep. 2020
Matt J
am 22 Sep. 2020
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
am 22 Sep. 2020
Bearbeitet: Dylan Tarter
am 22 Sep. 2020
>> 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{:}
Kategorien
Mehr zu Logical finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!