How to assign points to one or several boxes
조회 수: 1 (최근 30일)
이전 댓글 표시
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
2020년 1월 31일
Are you only going to have two boxes, or this need to scale up to arbitrary number of boxes ?
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
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{:});
댓글 수: 0
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!