How to use inpolygon function without applying the for loop?

3 visualizaciones (últimos 30 días)
Sourav Mukherjee
Sourav Mukherjee el 28 de Sept. de 2019
Editada: Sourav Mukherjee el 28 de Sept. de 2019
Hello everyone,
  1. I have the coordinates (x=lon_points, y=lat_points) for a set of points. All I want to check is if these points are located inside/on or outside a set of 0.5x0.5 grid boxes whose center coordinates are specified by (x=lon_gridbox, y=lat_gridbox).
2. The code that I am currently running by using the for-loop is as below. Although, I am performing this operation for a huge number of grid boxes, for the ease of representation I am only providing the code here for a limited number of grid boxes.
3. I want to perform this operation without using the for loop. The reason being that it is taking a hell lot of time for the huge number of grid boxes that I am actually operating on.
Can someone suggest a way to perform this operation in a faster way by avoiding the for loop?
%% Code starts here
lat_points=[45.40927; 45.40961; 45.40995; 45.41029; 45.41063; 45.41097; 45.41131; 45.41165]; % y coordinates of the points
lon_points=[-50.00022; -50.05021; -50.10021; -50.15020; -50.20019; -50.25019; -50.30018; -50.35018]; % x coordinate of the points
xv=[lon_gridbox(:,1)-0.25,lon_gridbox(:,1)+0.25,lon_gridbox(:,1)+0.25,lon_gridbox(:,1)-0.25,lon_gridbox(:,1)-0.25];% x coordinates of the vertices for each 0.5x0.5 grid box
yv=[lat_gridbox(:,1)-0.25,lat_gridbox(:,1)-0.25,lat_gridbox(:,1)+0.25,lat_gridbox(:,1)+0.25,lat_gridbox(:,1)-0.25];% y coordinates of the vertices for each 0.5x0.5 grid box
int=[];
for n=1:length(lat_gridbox(:,1))
[on in] = inpolygon(lat_points(:,1),lon_points(:,1),xv(n,:),yv(n,:)); % checking if the points fall outside or inside/on the grid box defined by the vertices, xv and yv.
int=[int;[lon_gridbox(n,1),lat_gridbox(n,1),in',on']];
end
%%% Code ends here
Thanks!

Respuestas (1)

David Hill
David Hill el 28 de Sept. de 2019
On = (ismember(lat_points,lat_gridbox-.25)+ismember(lat_points,lat_gridbox+.25))|(ismember(lon_points,lon_gridbox-.25)+ismember(lon_points,lon_gridbox+.25));
In = ~On;
Easy to determine if points are on the grid. If the points are not on the grid then they are inside the grid.
  1 comentario
Sourav Mukherjee
Sourav Mukherjee el 28 de Sept. de 2019
Editada: Sourav Mukherjee el 28 de Sept. de 2019
I am very sorry David about not being clear with my objective here.
My objective is : to check if these points are located inside/on or outside a set of 0.5x0.5 grid boxes whose center coordinates are specified by (x=lon_gridbox, y=lat_gridbox).
I have edited the question accordingly.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by