Help with using parfor (exceed matrix dimensions error)
Show older comments
I need some help with parallel programming. I can't figure out how to correctly index variables for the parfor loop. The main thing with this code is that I need to sum some logical values (variable 'in') throughout the iteration in defined positions (given by variable 'a') inside a matrix ('grid'). The code using for loops works just fine, but takes too much time to process, as I have really a large amount of data to be processed. I don't know if I am correct, but I would like to try using parfor and running the code in a multi core computer to get the results faster. However, I don't know if its possible to use it in my code, once I have to store the results in a very specific way. If you have any suggestions, please, let me know. The code is the one above and the error that I get running it is: "Error using matlab_question (line 17 --> first parfor loop) Index exceeds matrix dimensions."
dat = cell(50000,1);
gridcm = 5;
tamx = 50;
tamy = 60;
grid = cell(50000,1);
grid2 = zeros(gridcm*(tamx),gridcm*(tamy));
for i=1:size(dat,1)
dat{i,1} = rand(5,2);
dat{i,1} = [dat{i,1};dat{i,1}(1,:)];
end
tic
parfor t = 1:size(dat,1)
minx = floor(gridcm*min(dat{t,1}(:,1)));
maxx = ceil(gridcm*max(dat{t,1}(:,1)));
miny = floor(gridcm*min(dat{t,1}(:,2)));
maxy = ceil(gridcm*max(dat{t,1}(:,2)));
x = minx:maxx;
y = miny:maxy;
xi = repmat(x,length(y),1);
xi = reshape(xi,length(x)*length(y),1);
yi = repmat(y,length(x),1)';
yi = reshape(yi,length(y)*length(x),1);
a = [xi yi];
in = inpolygon(a(:,1),a(:,2),(dat{t,1}(:,1))*gridcm,(dat{t,1}(:,2))*gridcm);
gridIni= zeros(gridcm*(tamx),gridcm*(tamy));
grid{t,1} = zeros(gridcm*(tamx),gridcm*(tamy));
grid{t,1} = grid{t,1} + gridIni(a(t,1),a(t,2)) + in(t);
end
parfor i=1:length(grid)
mat = cell2mat(grid{i,1});
grid2 = grid2+mat;
end
toc
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!