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.

 채택된 답변

Dylan Tarter
Dylan Tarter 2021년 9월 14일

0 개 추천

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)

추가 답변 (2개)

Rik
Rik 2020년 9월 22일

2 개 추천

It looks like either blockproc or mat2cell is what you're looking for.

댓글 수: 1

Dylan Tarter
Dylan Tarter 2020년 9월 22일
편집: Dylan Tarter 2020년 9월 22일
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

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

Matt J
Matt J 2020년 9월 22일
편집: Matt J 2020년 9월 22일

0 개 추천

댓글 수: 6

Dylan Tarter
Dylan Tarter 2020년 9월 22일
Ive done this cell option before but I need something that can execute in the patterend order I specified. my confusion is less on generating a grouped subset and more on doing so in the order specified ive tried this mat2tiles and it doesnt let me do something to each tile individually but ill look deeper into the source code
Matt J
Matt J 2020년 9월 22일
편집: Matt J 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
Dylan Tarter 2020년 9월 22일
The pattern that that outputs is:
U(1,1) U(1,2), U(1,3) U(1,4),U(2,1) U(2,2), U(2,3) U(2,4) U(3,1) U(3,2), U(3,3) U(3,4) ,U(4,1) U(4,2) U(4,3) U(4,4)
What I discussed is:
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)
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
Dylan Tarter 2020년 9월 22일
편집: Dylan Tarter 2020년 9월 22일
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 2020년 9월 23일
편집: Matt J 2020년 9월 23일
>> 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에 대해 자세히 알아보기

태그

질문:

2020년 9월 22일

답변:

2021년 9월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by