Sub-matrix Access in a Square Pattern

2 views (last 30 days)
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.

Accepted Answer

Dylan Tarter
Dylan Tarter on 14 Sep 2021
While the suggested things are undoubtedly true, I found the answer a long while back. The behavior I am describing is actually that of a space filling Z-Curve. Simply performing a Z-curve transform will put the data into an order in which every 4 elements in order will be the desired grouping:
for i = 0:1
for j = 0:1
for x = 2*i:2*i+1
for y = 2*j:2*j+1
% repeat x and y loops for 2^n dimensions
end
end
end
end
Obviously this is not ideal for variable sizes, but you can use bitmasking math to mimic the behavior.
This causes a transform of such data to simply be 0,1,2,3 = 0,1,n,n+1 (as the first top left corner 2x2 grouping)

More Answers (2)

Rik
Rik on 22 Sep 2020
It looks like either blockproc or mat2cell is what you're looking for.
  1 Comment
Dylan Tarter
Dylan Tarter on 22 Sep 2020
Edited: Dylan Tarter on 22 Sep 2020
Yes, kind of! The blockproc does exactly what I need in terms of subsection processing but the order that it likes to do things is the 4 corners then steps inwards, not in the exact pattern id like. It works great for some of the stuff I need to do tho when order doesnt matter but when I output the bitstream this would do (for a 4x4) U(1,1),U(1,4),U(4,1),U(4,4) ...
instead of U(1,1) U(1,2) U(2,1) U(2,2), U(1,3) U(1,4) U(2,3) U(2,4) U(3,1) U(3,2) U(4,1) U(4,2) U(3,3) U(3,4) U(4,3) U(4,4)
Its kind of a like a recursive blocking method useful for processing wavelets
Side Note: ive used mat2cell before to do the blocking stuff but it has the same problem of the two of using matlab's pick order as opposed to this recursive blocking pick order but blockproc is a much cleaner solution

Sign in to comment.


Matt J
Matt J on 22 Sep 2020
Edited: Matt J on 22 Sep 2020
  6 Comments
Dylan Tarter
Dylan Tarter on 22 Sep 2020
Edited: Dylan Tarter on 22 Sep 2020
No that just scans horizontally in my original post i described scanning in a square pattern. I guess my original post was not inclusive enough ill edit it, I did reply to u with the corrdinate mapping for a higher order array. I attached a png showing exactly what its doing.
Matt J
Matt J on 23 Sep 2020
Edited: Matt J on 23 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{:}

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!