Parfor on increasing array

12 次查看(过去 30 天)
DIMITRY
DIMITRY 2015-10-28
回答: Edric Ellis 2015-10-29
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 个评论
Adam
Adam 2015-10-28
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.

请先登录,再进行评论。

回答(1 个)

Edric Ellis
Edric Ellis 2015-10-29
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

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by