Why 'for loop' is very slow even working on server

My 'for loop' looks like
iwant = cell(a,b);
for i =1:17
c = data(:,:,i);
for j = 1:b
idx = inpolygon(X(:),Y(:),S(j).X , S(j).Y);
iwant{i,j} = c(idx);
end
end
Where a =17,c = 420*940, X = 420*940, Y = 420*940 and b = 1*1 which is polygon and its size is (shape_area 56.4087, x= 1*679340, y = 1*679340).
This for loop is taking very long time even days, so how could I execute it in minimum time
Thank you in advance for any kind of help

 Réponse acceptée

Walter Roberson
Walter Roberson le 20 Oct 2018

1 vote

The idx results are the same for all i values for a given j. Loop over j precomputing them and then loop over i pulling out the appropriate entries.

9 commentaires

Dear Walter sir thank you for your response, So, how could I execute it in minimum time because it is taking more than a day
How long does it take to make one inpolygon call with that data?
All polygons are merged together and it is taking more and a day
What precision do you need? It would probably be more efficient to render the shape as a bitmap, but doing so would discretize the search.
For example given a unit square, an indefinitely thin shape might wander back and forth several times in the square but only covering (say) 80 percent of the unit square, and indefinitely thin query coordinates might happen to be in the 20 percent not occupied. Your current code calls a function that takes that into account, providing an answer as precise as practical considering floating point limitations. But in the same scenario if the shape boundaries were considered to be even 1/10 pixel thick then or the query points were considered to cover an area then you might get a different result. The case where the edges can be considered to have some thickness can sometimes lead to solutions involving rendering to bitmap that can then be efficient to query. However if the thickness deemed is small then the memory requirements can get too large.
Dear walter, I need more precise output. I exploded/unmerged the polygons and it took only few minutes to execute the same code but output is dispersed over 3075 pieces corresponding to each polygon. As we know each polygon has different size and same with output, so how I can arrange the dispersed output in a matrix something having 17 rows. The attached picture is depicting the result
I do not know what the attached output is intended to represent ?
Shakir Hussain
Shakir Hussain le 23 Oct 2018
Modifié(e) : Shakir Hussain le 23 Oct 2018
First I was trying execute the command with large size single merged polygon in which hundreds of small polygons (3075) were merged together and that was taking very long time. But when it unmerged the polygons and tried again and it was very quick to execute, so the speed issue is solved here.
But a new issue arise here as the output was disperse in 3075 pieces and I want to assemble it into single matrix having 17 rows and certain number of columns, so how I can do it?
I do not know what the individual entries in your cell array are intended to mean.
The test data has attached here, which I want to convert into a matrix having 17 rows and columns according to data size. I was facing problem to attached sample data here from last few days

Connectez-vous pour commenter.

Plus de réponses (1)

Bruno Luong
Bruno Luong le 20 Oct 2018

0 votes

As I mention in another thread you can get on FEX that can be 100 times faster than MATLAB inpolygon, let alone the newy polyshape

7 commentaires

The speed is ok for me when I explode (unmerge) the polygons but the output is disperse in cell with different number of row and column as shown in above attached picture. So the question is how I can assemble the output cell in a single matrix with it real position.
Please describe your data type with small example, MATLAB code (and optionally real data you are working on), not a screen capture that isn't helpful.
Shakir Hussain
Shakir Hussain le 25 Oct 2018
Modifié(e) : Shakir Hussain le 25 Oct 2018
I have explained in above comment and the test data has attached here which I want to convert in to a matrix having 17 rows and columns as per data size.
test{1,6} has 10 elements. test{1,7} has 4 elements. What do those elements mean ?
The data you uploaded is a cell of 17 x 3038; you have previously spoken of a cell 17 x 3075.
What size of output are you looking for? How do you want it to be calculated from the 17 x 3038 cell ?
Shakir Hussain
Shakir Hussain le 25 Oct 2018
Modifié(e) : Shakir Hussain le 25 Oct 2018
e.g test{1,6} has 10 element so I want to put theses 10 elements into 10 separate columns in serial and same rule applies on all. In result we will have much more columns than 3075. or Could we convert it into matrix (cell2mat is not working for it) or write as txt,csv or xlsx?
tt = cell2mat(cellfun(@transpose,test,'uniform',0));
Thank you so much sir Walter

Connectez-vous pour commenter.

Produits

Version

R2018a

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by