MATLAB: How to Classify the Variable for parfor program

image processingparallel computingParallel Computing Toolbox

Hi Everybody,
I am priya Now doing one research. I want execute my code parallel using parfor. But not able to Run It. I got a Error Msg. ??? Error: The variable mcode in a parfor cannot be classified. See Parallel for Loops in MATLAB, "Overview".
This is my sample code
parfor i1=1:1:n
disp(i1);
??? Error: The variable mcoder,mcodeg,mcodeb,a,e,gcoder,gcodeg,gcodeb in a parfor cannot be classified.
See Parallel for Loops in MATLAB, "Overview".
if (var(i1) < 25)
label(i1)=0;
mcoder(e)=mranr(i1);
mcodeg(e)=mrang(i1);
mcodeb(e)=mranb(i1);
mgacode=cat(3,mcoder,mcodeg,mcodeb);
e=e+1;
else
label(i1)=1;
[a b c d er]=rand1(value1r(i1,:),value2,value3,value4);
[a b c d eg]=rand1(value1g(i1,:),value2,value3,value4);
[a b c d eb]=rand1(value1b(i1,:),value2,value3,value4);
??? Error using ==>
parallel_function at 598
Error in ==>
parallel_function>make_general_channel/channel_general
at 872
Subscripted assignment
dimension mismatch.
% following code mismatch error
gcoder(a,:) = [a b c d er];
gcodeg(a,:) = [a b c d eg];
gcodeb(a,:) = [a b c d eb];
gcode=cat(3,gcoder,gcodeg,gcodeb);
a=a+1;
err=(er+eg+eb)/3;
if (err > llim)
eno=eno+1;
end
end
end
My code Run properly without parfor. But With Parfor I got so many error msg.
I am a new matlab user. How can I modify that code please any one help me my problem.
Thank You so much.

Best Answer

You are indexing arrays at "e", which is not initialized within the parfor loop. The value of "e" at any one point thus depends upon what the previous iterations of the loop have done. That makes indexing by "e" dependent on the sequential execution of the loops. "parfor" loops are executed in an unspecified order (currently they are launched in reverse order!), so any indexing within "parfor" must depend only on the loop variable and constants.
The same kind of problem occurs for your indexing by "a": parfor cannot know that your calls to rand1() are going to produce values that are dependent upon the loop variable "i1" so that it can be sure that indexing by "a" is not going to overlap locations with other iterations of the parfor loop. Indeed, considering that you do not even pass "i1" to rand1(), it seems quite unlikely that "a" will turn out to be unique for each "i1" value, so the parfor loop iterations would almost certainly attempt to write to the same locations within gcode* arrays.
Your output arrays need to be indexed at "i1" to prevent clashes with other iterations.
It is allowed for you to use a cell array that you index at "i1" and only sometimes store results in, and it is allowed for you to delete the unused (empty) cells after the end of the "parfor" loop. This would have the effect of allowing data to be added only "sometimes", with the end order being in increasing loop index order like you had for your "for" loop.