globals and parfor

From the documentation on parfor: The body of a parfor-loop cannot contain global or persistent variable declarations. In the context of your problem, i.e., calling a function within the parfor that in turn references a global, this translates into: “parfor will probably not give expected or meaningful results”. This makes perfect sense. Consider the following … Read more

How to nest multiple parfor loops

MrAzzman already pointed out how to linearise nested loops. Here is a general solution to linearise n nested loops. 1) Assuming you have a simple nested loop structure like this: %dummy function for demonstration purposes f=@(a,b,c)([a,b,c]); %three loops X=cell(4,5,6); for a=1:size(X,1); for b=1:size(X,2); for c=1:size(X,3); X{a,b,c}=f(a,b,c); end end end 2) Basic linearisation using a for … Read more