Best way to create a matrix of points?

38 ビュー (過去 30 日間)
Richárd Tóth
Richárd Tóth 2019 年 9 月 5 日
コメント済み: Bruno Luong 2019 年 9 月 5 日
Hello
I have a matrix that contains rectangles(squares in this case), for example:
A=[-5 -4.5 1.5 2; -5 -4.5 2 2.5; -5 -4.5 2.5 3; -4.5 -4 1.5 2; -4.5 -4 2 2.5; -4.5 -4 2.5 3];
for i=1:6
rectangle('Position',[A(i,1) A(i,3) 0.5 0.5]);
hold on;
end
hold on;
axis([-10 10 -10 10])
Each row contains ( ) coordinates.
So in this case the result matrix should contain 12 rows, each represents 1 point, like:
-5 1.5
-5 2
-5 2.5
-5 3
-4.5 1.5
-4.5 2
-4.5 2.5
-4.5 3
-4 1.5
-4 2
-4 2.5
-4 3
The rectangles are not guranteed to form another rectangle, a more realistic set of rectangles:
rect.GIF
  1 件のコメント
Richárd Tóth
Richárd Tóth 2019 年 9 月 5 日
編集済み: Richárd Tóth 2019 年 9 月 5 日
I think I will simply iterate through the rectangles and add all 4 of their points to the result matrix, then select unique rows from the result. If you have a more efficient solution, let me know

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

採用された回答

Guillaume
Guillaume 2019 年 9 月 5 日
編集済み: Guillaume 2019 年 9 月 5 日
A=[-5 -4.5 1.5 2; -5 -4.5 2 2.5; -5 -4.5 2.5 3; -4.5 -4 1.5 2; -4.5 -4 2 2.5; -4.5 -4 2.5 3];
points = unique([A(:, [1, 3]); A(:, [1, 4]); A(:, [2, 3]); A(:, [2, 4])], 'rows')
is the simplest though posibly not the most efficient.
edit: this may be marginally more efficient, though more obscure:
points = unique(reshape(A(:, [1 1 2 2 3 4 3 4])), 'rows')
  3 件のコメント
Guillaume
Guillaume 2019 年 9 月 5 日
Oops! How did that happen, should have been:
points = unique(reshape(A(:, [1 1 2 2 3 4 3 4]), [], 2), 'rows')
Bruno Luong
Bruno Luong 2019 年 9 月 5 日
I don't know how A is generated but you might cautious to replace UNIQUE with UNIQUETOL in case two adjacent rectangles do not have the exact match of coordinates due to round-off.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by