MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

# How can i classified the variable inside of the parfor loop?

Asked by PRIYANGA on 28 Jan 2013

Sir, This is my PARFOR loop coding. I can't classified some variables in PARFOR LOOP.

```parfor i1=1:1:nrr1
```
`      disp(i1);`
`      if (varran(i1) < threshold)`
`          labe(i1)=0;`
`          mvqcoder(i1)=[meanranr(i1)];`
`          mvqcodeg(i1)=[meanrang(i1)];`
`          mvqcodeb(i1)=[meanranb(i1)];`
```%         e=e+1;
```
`      else`
`          labe(i1)=1;`
`          [domr]=domsearch(rang1r(i1,:),dompoolr,gsize,ci1r(i1),ci2r(i1),ci3r(i1),msr(i1),mdr,cd1r,cd2r,cd3r,mscr);`
`          [domg]=domsearch(rang1g(i1,:),dompoolg,gsize,ci1g(i1),ci2g(i1),ci3g(i1),msg(i1),mdg,cd1g,cd2g,cd3g,mscg);`
`          [domb]=domsearch(rang1b(i1,:),dompoolb,gsize,ci1b(i1),ci2b(i1),ci3b(i1),msb(i1),mdb,cd1b,cd2b,cd3b,mscb);`
`          dom=[domr,domg,domb];`
`          [rz cz]=size(dom);`
`          meanrar=mean(rang1r(i1,:));`
`          meanrag=mean(rang1g(i1,:));`
`          meanrab=mean(rang1b(i1,:));`
`        dompolr=iCalc(dompoolr,dom,cz);`
`        dompolg=iCalc(dompoolg,dom,cz);`
`        dompolb=iCalc(dompoolb,dom,cz);`
```[isomer alphr meanrr dom1r errr]=affwerrqt(rang1r(i1,:),dompolr(1:cz,:),gsize);
```
```[isomeg alphg meanrg dom1g errg]=affwerrqt(rang1g(i1,:),dompolg(1:cz,:),gsize);
```
```[isomeb alphb meanrb dom1b errb]=affwerrqt(rang1b(i1,:),dompolb(1:cz,:),gsize);
```
```          cdom1r(i1)=dom(dom1r);
isome1r(i1)=isomer;
alph1r(i1)=alphr;
meanr1r(i1)=meanrar;```
```          cdom1g(i1)=dom(dom1g);
isome1g(i1)=isomeg;
alph1g(i1)=alphg;
meanr1g(i1)=meanrag;```
```          cdom1b(i1)=dom(dom1b);
isome1b(i1)=isomeb;
alph1b(i1)=alphb;
meanr1b(i1)=meanrab;```
`              vqcode3r(i1,:)=[isome1r(i1),alph1r(i1),meanr1r(i1),cdom1r(i1)];`
`              vqcode3g(i1,:)=[isome1g(i1),alph1g(i1),meanr1g(i1),cdom1g(i1)];`
`              vqcode3b(i1,:)=[isome1b(i1),alph1b(i1),meanr1b(i1),cdom1b(i1)];`
```%             c=c+1;
```
```          end
%         w1=w1+1;
end```

I got Error result

```The temporary variable   meanrar is used after the PARFOR loop but its value is non deterministic.
```
```The temporary variable   meanrag is used after the PARFOR loop but its value is non deterministic.
```
```The temporary variable   meanrab is used after the PARFOR loop but its value is non deterministic.
```

then,

```The PARFOR loop can't run due to the way variable dompolr is used.
```
```The PARFOR loop can't run due to the way variable dompolg is used.
```
```The PARFOR loop can't run due to the way variable dompolb is used.
```

Please any one help this problem for parfor loop.

thank you

PRIYANGA on 29 Jan 2013

I can't understand this problem sir. How can I refer the variable after that loop. Please tell me sir.

how can i rectify that problem sir.

This is my original code sir. i have to change my code to parallel processing followed your procedure already told you another one program. But some errors occurred in my code sir. please give me solution sir.

"The temporary variable meanrar is used after the PARFOR loop but its value is non deterministic."

PRIYANGA on 30 Jan 2013

Sir give a solution for this problem sir

Randy Souza on 12 Feb 2013

I have restored the original text of this question.

PRIYANGA, this question has a clear subject and an accepted answer, so it may be valuable to someone else in the future. If you have a good reason why it should be removed from MATLAB Answers, please flag the question, explain why it should be deleted, and an administrator or high-reputation contributor will consider deleting the question. Please do not simply edit your question away.

## Products

Answer by Edric Ellis on 28 Jan 2013

The problem is that you're building e.g. dompolr in a loop which the PARFOR machinery cannot understand. Your code currently looks a bit like this:

```parfor i1 = 1:N
...
for z = 1:cz
dompolr(z,:) = dompoolr(dom(z),:);
end
...
end
```

```parfor i1 = 1:N
...
dompolr = iCalc(dompoolr,dom,cz);
...
end
```
```...
```
```function val = iCalc( dompoolx, dom, cz )
for z = 1:cz
val(z,:) = dompoolx(dom(z),:);
end
```

PRIYANGA on 29 Jan 2013

I have to apply this function, but i got a message for

" The temporary variable dompolr is used after the PARFOR loop but its value is non deterministic."

This is my original code sir without parfor

how can i rectify that problem sir.

PRIYANGA on 30 Jan 2013

Sir give a solution for this problem sir

Edric Ellis on 1 Feb 2013

Any variables that are completely written to within the PARFOR loop are 'temporary', and therefore you cannot rely on the value after the loop has completed. For example

```parfor ii = 1:3
x = ii;
end
disp(x)
```

In this case, the value of 'x' cannot be known because it depends on the order of evaluation of the PARFOR loop (which is not deterministic). So, you must not use temporary variables after the PARFOR loop - instead, all outputs must either be sliced (i.e. indexed using the loop variable), or reductions, like

```x = 0;
parfor ii=1:3
x = x + ii;
end
disp(x);
```