2017-05-04 3 views
2

데이터 포인트가 있으며 MATLAB의 그림으로 쉽게 그릴 수 있습니다.점이 사각형 내에 있는지 확인하십시오.

첨부 된 그림에서 볼 수 있듯이 어떤 데이터 포인트가 사각형 영역 안에 있는지 알아야합니다. 이 그림에서 검은 점은 데이터 점을 나타내고 빨간색 직사각형은 언급 된 점을 나타냅니다.

데이터 포인트를 검색하여 해당 데이터 포인트가 사각형에 속하는지 여부를 어떻게 확인할 수 있습니까? 각 사각형에 대한 모든 구성원 (데이터 요소)의 목록이 필요합니다.

샘플 데이터 포인트와 사각형 : Ozcan이 코멘트에 말했듯이

Sample data points and rectangles

+0

"inpolygon"기능을 사용하십시오. – Ozcan

+0

@Ozcan 감사합니다. 그게 내가 원하는거야! 나는 그런 기능이 존재한다고 생각조차 할 수 없었다! – Mechatronics

+0

여기에서 MATLAB의 함수 목록을 확인할 수 있습니다. https://www.mathworks.com/help/matlab/functionlist.html – Ozcan

답변

3

inpolygon가 갈 수있는 방법입니다. 여기에 빠른 데모입니다, 자세한 내용은 주석을 참조하십시오

% Create 4 random rectangles, defined by their x and y coords in rectX and rectY. 
% Each column defines a different rectangle. 
sizes = randi([5,10], 2, 4)./10; 
rectX = randi([1,5], 1, 4); rectX = [rectX; rectX; rectX + sizes(1,:); rectX + sizes(1,:)]; 
rectY = randi([1,5], 1, 4); rectY = [rectY; rectY + sizes(2,:); rectY + sizes(2,:); rectY]; 
% Create a random set of 1000 points for testing 
points = [rand(1000, 1)*range(rectX(:))+min(rectX(:)), rand(1000, 1)*range(rectY(:))+min(rectY(:))]; 
% Set up logical matrix of test results 
inrect = logical(zeros(size(points,1), size(rectX,2))); 
% Plot the rectangles using patch 
    figure; 
patch(rectX,rectY,'red') 
% Hold on and plot all of the points as black dots 
hold on; 
plot(points(:,1),points(:,2),'.k'); 
% Loop through each rectangle, testing the points 
for r = 1:size(rectX, 2) 
    % Test points using inpolygon, store results to inrect matrix 
    inrect(:,r) = inpolygon(points(:,1), points(:,2), rectX(:,r), rectY(:,r)); 
end 
% Plot all points which are in any rectangle as blue circles 
plot(points(any(inrect,2), 1), points(any(inrect,2), 2), 'bo'); 

결과 :

plot


참고 이제 사각형 당 하나의 열에서 (사실 논리적 매트릭스 inrect을 가지고, 점이 사각형 안에있을 때 점 당 하나의 행). 위 코드는 포인트가 인 경우 플로팅에 any 연산자를 사용하여 모든 직사각형을 나타냅니다.

관련 문제