MATLAB Answers


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

Asked by Shakir Hussain on 20 Oct 2018
Latest activity Commented on by Shakir Hussain on 26 Oct 2018
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);
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

  1 Comment


Sign in to comment.





2 Answers

Answer by Walter Roberson
on 20 Oct 2018
 Accepted Answer

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.


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

Sign in to comment.

Answer by Bruno Luong
on 20 Oct 2018

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


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

Sign in to comment.