MATLAB: How do i run this parallel programming using parfor

parallel computingparfor

i'm trying to run insertion sort as a parallel programming using parfor, but it gives me some kind of error which I cant fix, here is the code:-
clear A
t=cputime;
x = 1 : 100;
n = length(x);
parfor j = 2:n
pivot = x(j);
i = j;
while ((i > 1) && (x(i - 1) > pivot))
x(i) = x(i - 1);
i = i - 1;
end
x(i) = pivot;
end
cputime-t
Error: The variable x in a parfor cannot be classified.
See Parallel for Loops in MATLAB, "Overview".

Best Answer

It is not allowed to access the distributed variable at two different indices.
The code you are doing that moves the pivot earlier and earlier would have different results depending upon the iterations for the earlier j had already been run or not. That is not allowed: every parfor iteration needs to be independent of the other iterations so that the order the iterations are run in does not matter (except for accumulated round-off error in "reduction variables".)
Related Question