MATLAB: For loops nested inside parfor loops.

parfor

Hallo everyone, I have a bit of a problem with some parfor coding. The concept is this:
parfor j = 1:1:10;
for i = 1:1:10;
Q(i) = i^2;
end
R = Q(j);
end
Of course this code is non-sensicle, but it illustrates the problem. I cannot create an array of values inside a parfor loop that I can then use throughout the parfor loop. In a practical application the expression for Q(i) will depend on j, so taking the sub-loop outside the parfor in not an option. Therefore, for now I wish to know why such a simple example can't be parallelised. As far as the algorithm is concerned it has to individually loop through i and then compute R in parallel with no obvious interdependency between the loops. Many thanks.

Best Answer

Hi,
if you add the following line before the inner loop, it should work:
Q = zeros(10, 1);
Titus