Asked by PRIYANGA
on 30 Jan 2013

This is my original code without parfor

For i1=1:1:nrr1

disp(i1);

if (varran(i1) < threshold)

labe(i1)=0;

mvqcoder(e)=[meanranr(i1)];

mvqcodeg(e)=[meanrang(i1)];

mvqcodeb(e)=[meanranb(i1)];

e=e+1;

mvqcode=cat(3, mvqcoder, mvqcodeg, mvqcodeb); 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,:));

meanra=cat(3,meanrar ,meanrag,meanrab);

for z=1:1:cz dompolr(z,:)=dompoolr(dom(z),:); end for z=1:1:cz dompolg(z,:)=dompoolg(dom(z),:); end for z=1:1:cz dompolb(z,:)=dompoolb(dom(z),:); end dompol=cat(3,dompolr,dompolg,dompolb);

[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(w1)=dom(dom1r); isome1r(w1)=isomer; alph1r(w1)=alphr; meanr1r(w1)=meanrar;

cdom1g(w1)=dom(dom1g); isome1g(w1)=isomeg; alph1g(w1)=alphg; meanr1g(w1)=meanrag;

cdom1b(w1)=dom(dom1b); isome1b(w1)=isomeb; alph1b(w1)=alphb; meanr1b(w1)=meanrab;

cdom1=cat(3,cdom1r,cdom1g,cdom1b); isome1=cat(3,isome1r,isome1g,isome1b); alph1=cat(3,alph1r,alph1g,alph1b); meanr1=cat(3,meanr1r,meanr1g);

vqcode3r(c,:)=[isome1r(w1),alph1r(w1),meanr1r(w1),cdom1r(w1)]; vqcode3g(c,:)=[isome1g(w1),alph1g(w1),meanr1g(w1),cdom1g(w1)]; vqcode3b(c,:)=[isome1b(w1),alph1b(w1),meanr1b(w1),cdom1b(w1)]; c=c+1; vqcode3=cat(3,vqcode3r,vqcode3g,vqcode3b); end w1=w1+1; end

Any one please give me a solution for this problem .

Answer by Walter Roberson
on 30 Jan 2013

Accepted answer

The part of that code that can be translated to use "parfor" would come out as,

parfor i1=1:1:nrr1

if (varran(i1) < threshold)

labe(i1)=0; else labe(i1)=1; end end

The rest of the code in your "for" loop depends upon the order of executing the iterations and so cannot be changed to use "parfor".

Walter Roberson
on 30 Jan 2013

You need to go through the code and figure out which variables you want to save the i1'th version of, and which variables you only need temporarily during any particular i1 iteration, and which variables you need the (i1-1)'th version of to calculate the i1'th version. If there *are* any variables that need the (i1-1)'th version to calculate the i1'th version, that the calculation of those variables cannot be done within parfor.

Have a look at, for example, vqcode3 : you overwrite it in every iteration of i1, and you never use it in computation, so the sequential "for" is going to end up with vqcode3 being what was calculated for vqcode3 in the last iteration of i1 (when i1 = nrr1). You cannot simply code that into "parfor" because "parfor" can execute the iterations in any order. You *could* use something like

if i1 == nrr1 vqcode3{i1} = ... the value end

and not write anything in the other iterations. But if you were going to do that you might as well just compute it after the entire parfor loop based upon the variables you did save.

If you are not calculating vqcode3 for any but the last iteration, then there are other variables that do not need to be calculated, and then since those are not needed, other variables do not need to be calculated either. You might find that a fair bit of what you are calculating now is not needed inside the loop.

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

Learn moreOpportunities for recent engineering grads.

Apply Today
## 3 Comments

## Jing (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/60316#comment_125952

For using parfor loop, the task within the loop should be independent, that is the ith task does not depend on the (i-1)th task before.

There are several index variables, which I'm not sure whether they should appear in order. If they're ordered, you can't use parfor for this problem.

## PRIYANGA (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/60316#comment_125954

I have to change some index variable in parallel code, But i am not sure that solution. I don't know how to change the proper way. You know that way please give me solution sir.

## Randy Souza (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/60316#comment_129013

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 notsimply edit your question away.