Group 3 * 3 (2d) or 3 * 3 * 3 (3d) coordinates into square blocks?

Hi all,
This question might be quite complex. Imagine there is a 3D block like this with the Cartesian coordinates:
There are 27 nodes, corresponding coordinates are (first column denotes a linear index):
K>> coords
coords =
1 -1 -1 -1
2 -1 0 -1
3 -1 1 -1
4 0 -1 -1
5 0 0 -1
6 0 1 -1
7 1 -1 -1
8 1 0 -1
9 1 1 -1
10 -1 -1 0
11 -1 0 0
12 -1 1 0
13 0 -1 0
14 0 0 0
15 0 1 0
16 1 -1 0
17 1 0 0
18 1 1 0
19 -1 -1 1
20 -1 0 1
21 -1 1 1
22 0 -1 1
23 0 0 1
24 0 1 1
25 1 -1 1
26 1 0 1
27 1 1 1
Now I'd like to group these 27 coordinates into small sub-blocks. I labelled these sub-blocks with red numbers, but it's not a must to follow the label sequence. The results should be:
block1:
-1 -1 -1
0 -1 -1
0 -1 0
-1 -1 0
-1 0 -1
0 0 -1
0 0 0
-1 0 0
block2
0 -1 -1
1 -1 -1
1 -1 0
0 -1 0
0 0 -1
1 0 -1
1 0 0
0 0 0
block3
......
Also, the code should work for any dimension, i.e. if there is a 2D square:
The code should be able to sort the coordinates into 4 sub-squares.
Can anyone help me with it? Or is there a toolbox developed for this kind of problem? Really appreciate it!

 採用された回答

Matt J
Matt J 2017 年 11 月 10 日
編集済み: Matt J 2017 年 11 月 10 日

0 投票

The result is the cell array "blocks":
dim=size(coords,2)-1;
L=(dec2bin(0:2^dim-1)-'0')*2-1;
N=size(L,1);
blocks=cell(1,N);
T=coords(:,2:end);
for i=1:N
idx=all(bsxfun(@times,T,L(i,:))>=0,2);
blocks{i}=T(idx,:);
end

3 件のコメント

Xiaohan Du
Xiaohan Du 2017 年 11 月 10 日
This is awesome, is it possible to make the output coordinates anti-clockwise? I check ed the 2D case, it works well but the output coordinates do not follow a rotational direction.
Current output is
>> blocks{:}
ans =
-1 -1
-1 0
0 -1
0 0
ans =
-1 1
-1 0
0 0
0 1
ans =
1 -1
0 -1
0 0
1 0
ans =
1 1
0 0
0 1
1 0
Anti-clockwise output would be:
>> otpt{:}
ans =
1 -1 -1
6 0 -1
7 0 0
5 -1 0
ans =
5 -1 0
7 0 0
8 0 1
2 -1 1
ans =
6 0 -1
3 1 -1
9 1 0
7 0 0
ans =
7 0 0
9 1 0
4 1 1
8 0 1
Really appreciate it!
Matt J
Matt J 2017 年 11 月 10 日
編集済み: Matt J 2017 年 11 月 10 日
In each block, you could compute the angle of the points using atan2() and then sort them according to that.
Xiaohan Du
Xiaohan Du 2017 年 11 月 10 日
Thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeAerospace Blockset についてさらに検索

質問済み:

2017 年 11 月 9 日

コメント済み:

2017 年 11 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by