Parfor on increasing array

10 views (last 30 days)
DIMITRY
DIMITRY on 28 Oct 2015
Answered: Edric Ellis on 29 Oct 2015
Hi World,
I would like to know how to reduce a computing time from hours...and hours to less time... In fact I have an increasing array define like X =[]; that is increasing at each iteration from a for loop X(end+1,:) = [Z T V];
I would like to know how it would be possible to use a parfor loop on an increasing array as I have an error message using it in a such case. 'THE PARFOR loop cannot run due to the way X is used' !
Regards,
  1 Comment
Adam
Adam on 28 Oct 2015
You need to have a fixed sized array to run a parfor loop. If it keeps resizing in the middle of processing the other workers will not be able to function correctly.
Usually a loop that is slow that has a resizing array is slow precisely because of the resizing array. If you can estimate an upper bound on the size the array can reach then predeclare it at that size and trim off the unused part at the end instead.

Sign in to comment.

Answers (1)

Edric Ellis
Edric Ellis on 29 Oct 2015
parfor does support increasing arrays in this way, even if it is not necessarily desirable. To do that though, you must use [] to concatenate together the pieces. So, for example, you could do something like this:
x = [];
parfor idx = 1:10
x = [x; rand(1, 3)];
end
This is explained in the doc relating to "reductions".
However, if you know in advance how large x needs to be (as in the example above), it's much better to do something more like
x = zeros(10, 3);
parfor idx = 1:10
x(idx, :) = rand(1, 3);
end

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!