How to improve performance of huge cellfun + inpolygon?
Afficher commentaires plus anciens
Dear all,
I have these variables:
B: matrix 19,381x31,768, contains my query points (this variable is not included in the 'test.mat')
lat: vector 19,381x1, contains latitudes of the query points
lon: vector 31,768x1, contains longitudes of the query points
PgonsLat: 5x5 cell array, each cell contains 5 latitudes that define the edges of a polygon
PgonsLon: 5x5 cell array, each cell contains 5 longitudes that define the edges of a polygon
I want to find which query points lie inside each of the polygons (the resulting 'inp1' needs to be cell array). So I use inpolygons function (https://uk.mathworks.com/matlabcentral/fileexchange/7187-inpolygons?s_tid=srchtitle_support_results_2_inpolygons), which has the same syntax as MATLAB inpolygon function (but is faster, I guess).
I wrote this code:
coord = NaN(length(lat)*length(lon),2);
for i=1:length(lat)
for j=1:length(lon)
coord(j+((i-1)*(length(lon)-1)),:)=[lat(i),lon(j)];
end
end
checkInsidePolygon1=@(llat,llon) inpolygons(coord(:,1),coord(:,2),llat,llon);
inp1=cellfun(checkInsidePolygon1, PgonsLat,PgonsLon,'UniformOutput',false);
It works, but the performance is poor. The line below takes 184 seconds.
>> tic
inp1=cellfun(checkInsidePolygon1, PgonsLat,PgonsLon,'UniformOutput',false);
toc
>> Elapsed time is 184.079170 seconds.
Unfortunately this line is inside a loop, and I need to iterate over 35,000 times. So it would take weeks to finish.
Is there any way to speed this up?
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Mathematics dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!