How to assign points to one or several boxes

조회 수: 1 (최근 30일)
012786534
012786534 2020년 1월 31일
댓글: 012786534 2020년 1월 31일
Hi,
I have a small function that assign points to boxes based on latitude and longitude. This code mostly works: point A falls into box A and point B falls into box B. The main flaw is that points that fall on overlaping boxes (like point C which falls into both box A and box B) are only assigned the name of one box (box B in this case). How can I modify this code so that points that fall on overlapping boxes are assigned the name of both boxes ?
Please note that points alway fall into at least one box so the possibility of a point not being assigned any box does not exist.
Thank you,
% Small function to assign points to boxes
% Points positions
lat = [47.5, 45.5, 46.5]';
lon = [-63.5, -61.5, -62.5]';
pts_name = {'A'; 'B'; 'C'};
pts_positions = table(pts_name, lat, lon);
% Min and max values of boxes
box_name = {'box_A'; 'box_B'};
min_lat = [46, 45]';
max_lat = [48, 47]';
min_lon = [-64, -63]';
max_lon = [-62, -61]';
boxes_positions = table(box_name, min_lat, max_lat, min_lon, max_lon);
% Assign points to boxes based on latitude/longitude
for g = 1:height(boxes_positions)
h = boxes_positions(g, 1);
mask = pts_positions.lon > boxes_positions.min_lon(g) & pts_positions.lon < boxes_positions.max_lon(g) & ...
pts_positions.lat > boxes_positions.min_lat(g) & pts_positions.lat < boxes_positions.max_lat(g);
pts_positions.box(mask) = table2cell(h(1, 1));
end
  댓글 수: 2
Mohammad Sami
Mohammad Sami 2020년 1월 31일
Are you only going to have two boxes, or this need to scale up to arbitrary number of boxes ?
Mohammad Sami
Mohammad Sami 2020년 1월 31일
Also can we assume that in your next step you are going to look up by boxes. E.g for point p1, you will classify it into box(es) and then lookup the box(es) to find all the nearby points ?

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

채택된 답변

Mohammad Sami
Mohammad Sami 2020년 1월 31일
Would this be fine. The point C will be repeated twice as in two boxes.
lat = [47.5, 45.5, 46.5]';
lon = [-63.5, -61.5, -62.5]';
pts_name = {'A'; 'B'; 'C'};
pts_positions = table(pts_name, lat, lon);
% Min and max values of boxes
box_name = {'box_A'; 'box_B'};
min_lat = [46, 45]';
max_lat = [48, 47]';
min_lon = [-64, -63]';
max_lon = [-62, -61]';
boxes_positions = table(box_name, min_lat, max_lat, min_lon, max_lon);
% Assign points to boxes based on latitude/longitude
assigned = cell(height(boxes_positions),1);
for g = 1:height(boxes_positions)
mask = pts_positions.lon > boxes_positions.min_lon(g) & pts_positions.lon < boxes_positions.max_lon(g) & ...
pts_positions.lat > boxes_positions.min_lat(g) & pts_positions.lat < boxes_positions.max_lat(g);
temp = pts_positions(mask,:);
temp.box = repmat(box_name(g),height(temp),1);
assigned{g} = temp;
end
pts_position_assigned = vertcat(assigned{:});

추가 답변 (1개)

012786534
012786534 2020년 1월 31일
In reality there is a about 50 boxes. Altought few of them overlap, I still need to take into account the case where a point falls into two boxes. There are no cases where more than two boxes overlap.

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by