# How to improve the speed of "inpolygon"?

I have about 30,000 pairs of longitude and latitude data (lon1 and lon2). My goal is to check if they will fall into a polygon defined by (lonW and latW).

In Matlab, I can do this as below. It is blazing fast. It literally takes no time to finish.
`in = inpolygon(lon1, lat1, lonW, latW);`

Below is how I’m doing it in Julia.

``````using PolygonOps
using StaticArrays
polygon = SVector.(lonW, latW);
in = [inpolygon((x, y), polygon; in=true, on=false, out=false) for x in lon1, y in lat1];
``````

It will take forever to finish. In fact I’m never able to wait long enough for it to complete.It will make my computer to run very hot and noisy. As you know, this is not normal. Julia is supposed to be faster than Matlab. Anyone knows a faster way to process this in Julia? Many thanks.

You want `for (x,y) in zip(lon1, lat1)`. `for x in lon1, y in lat1` constructs the Cartesian product, so it’s doing way more work.

3 Likes

Are you sure you want 90.000.000 pairs of checking?

1 Like

Do you know how the algorithm of Matlab works?

I mean, by analyzing the points separately, you can maybe reduce the problem?
Also, if the polygon is convex you can exclude many, many points (convex hull, etc.)

Point in polygons is actually a pretty old and famous problem so I wouldn’t wonder if Matlab has sophisticated algorithms for that.

1 Like

Many thanks, Oscar! That solved my issue beautifully. Now it is running blazingly fast. I’m surprised by the big difference!

Unfortunately, I don’t.