0
votes

I'm trying to parallelize the following code:

for m = 1:t
  dR((m-1)*3+1) = temp1(m);
  dR((m-1)*3+2) = temp2(m);
  dR(m*3) = temp3(m);
end

temp1, temp2, temp3 are just vecoters with size of m X 1. The size of dR is 3m X 3m.

Whenever I use parfor instead of for, it the code indicate errors in dR. I am not sure why. Please give me some suggestions.

Thank you!

1

1 Answers

1
votes

You're not correctly "slicing" dR. For parfor to run, output variables need to be indexed according to the rules described here. To fix your code, you could do this:

dR = zeros(t, 3);
parfor m = 1:t
    dR(m, 1:3) = [temp1(m), temp2(m), temp3(m)];
end

This then has the correct form of indexing for dR - i.e. a fixed index listing, and the loop variable appears as one of the subscripts.