How to assign points to one or several boxes
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
012786534
am 31 Jan. 2020
Kommentiert: 012786534
am 31 Jan. 2020
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 Kommentare
Mohammad Sami
am 31 Jan. 2020
Are you only going to have two boxes, or this need to scale up to arbitrary number of boxes ?
Mohammad Sami
am 31 Jan. 2020
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 ?
Akzeptierte Antwort
Mohammad Sami
am 31 Jan. 2020
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 Kommentare
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!