Pergunta

I am quite new to Matlab. I use parfor loop to do an extremely time-consuming task. See the snippet below. However, I got the error info from Matlab. Can anyone help? I read the document about parfor but do not see what to do...

Thank you.

The parfor loop cannot run due to the way variable "M" is used

The parfor loop cannot run due to the way variable "T" is used


Explanation 
MATLAB runs loops in parfor functions by dividing the loop iterations into groups, and then sending them to MATLAB workers where they run in parallel. For MATLAB to do this in a repeatable, reliable manner, it must be able to classify all the variables used in the loop. The code uses the indicated variable in a way that is incompatible with classification.




 parfor i=1:size(x,1)

    if (ind(index(i)) == Index1)
        para=lsqcurvefit(F, [M(index(i)) T(index(i))], t, SS(ind(index(i)):end,i), [0 0], [MaxiM(index(i)) maxT],options);
    elseif  (ind(index(i)) == Index2) 
        para=lsqcurvefit(F, [M(index(i)) T(index(i))], t2, SS(ind(index(i)):end,i), [0 0], [MaxiM(index(i)) maxT],options);
    end

end
Foi útil?

Solução

You should reorganize M and T in order to use them in a parallel loop. This should work:

M = M(index);
T = T(index);
parfor i=1:size(x,1)
    if (ind(index(i)) == Index1)
        para = lsqcurvefit(F, [M(i) T(i)], t, SS(ind(index(i)):end,i), ...
                           [0 0], [MaxiM(index(i)) maxT], options);
    elseif (ind(index(i)) == Index2) 
        para = lsqcurvefit(F, [M(i) T(i)], t2, SS(ind(index(i)):end,i), ...
                           [0 0], [MaxiM(index(i)) maxT], options);
    end
end

However, if you need return of function lsqcurvefit - then I would agree with the comment by Kleist that your code is meaningless.

UPDATE:

You can try to do similar rearrangements to further increase performance:

M = M(index);
T = T(index);
ind = ind(index);
MaxiM = MaxiM(index);
parfor i=1:size(x,1)
    if (ind(i) == Index1)
        para = lsqcurvefit(F, [M(i) T(i)], t, SS(ind(i):end,i), ...
                           [0 0], [MaxiM(i) maxT], options);
    elseif (ind(i) == Index2) 
        para = lsqcurvefit(F, [M(i) T(i)], t2, SS(ind(i):end,i), ...
                           [0 0], [MaxiM(i) maxT], options);
    end
end
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top