i want to generalize and reduce following program into one loop
2 views (last 30 days)
Show older comments
chx1=genx1 chx2=genx2 chx3=genx3 chx4=genx4 chx5=genx5
chy1=geny1 chy2=geny2 chy3=geny3 chy4=geny4 chy5=geny5
if ((gudx1>0) & (gudx2>0)) if (chx1==genx1) chx1= gudx2; end end
if ((gudx1>0) & (gudx3>0)) if (chx1==genx1) chx1= gudx3; end end
if ((gudx1>0) & (gudx4>0)) if (chx1==genx1) chx1= gudx4; end end
if ((gudx1>0) & (gudx5>0)) if (chx1==genx1) chx1= gudx5; end end
if ((gudx2>0) & (gudx3>0)) if (chx2==genx2) chx2= gudx3; end end
if ((gudx2>0) & (gudx4>0)) if (chx2==genx2) chx2= gudx4; end end
if ((gudx2>0) & (gudx5>0)) if (chx2==genx2) chx2= gudx5; end end
if ((gudx3>0) & (gudx4>0)) if (chx3==genx3) chx3=gudx4; end end
if ((gudx3>0) & (gudx5>0)) if (chx3==genx3) chx3=gudx5; end end
if ((gudx4>0) & (gudx5>0)) if (chx4==genx4) chx4= gudx5; end end
if ((gudx5>0) & (gudx1>0)) if (chx5==genx5) chx5= gudx1; end end
if ((gudx5>0) & (gudx2>0)) if (chx5==genx5) chx5= gudx2; end end
if ((gudx5>0) & (gudx3>0)) if (chx5==genx5) chx5= gudx3; end end
if ((gudx5>0) & (gudx4>0)) if (chx5==genx5) chx5= gudx4; end end
%y
if ((gudy1>0) & (gudy2>0)) if (chy1==geny1) chy1= gudy2; end end
if ((gudy1>0) & (gudy3>0)) if (chy1==geny1) chy1= gudy3; end end
if ((gudy1>0) & (gudy4>0)) if (chy1==geny1) chy1= gudy4; end end
if ((gudy1>0) & (gudy5>0)) if (chy1==geny1) chy1= gudy5; end end
if ((gudy2>0) & (gudy3>0)) if (chy2==geny2) chy2= gudy3; end end
if ((gudy2>0) & (gudy4>0)) if (chy2==geny2) chy2= gudy4; end end
if ((gudy2>0) & (gudy5>0)) if (chy2==geny2) chy2= gudy5; end end
if ((gudy3>0) & (gudy4>0)) if (chy3==geny3) chy3=gudy4; end end
if ((gudy3>0) & (gudy5>0)) if (chy3==geny3) chy3=gudy5; end end
if ((gudy4>0) & (gudy5>0)) if (chy4==geny4) chy4= gudy5; end end
if ((gudy5>0) & (gudy1>0)) if (chy5==geny5) chy5= gudy1; end end
if ((gudy5>0) & (gudy2>0)) if (chy5==geny5) chy5= gudy2; end end
if ((gudy5>0) & (gudy3>0)) if (chy5==geny5) chy5= gudy3; end end
if ((gudy5>0) & (gudy4>0)) if (chy5==geny5) chy5= gudy4; end end
ch1=[chx1 chy1] ch2=[chx2 chy2] ch3=[chx3 chy3] ch4=[chx4 chy4] ch5=[chx5 chy5]
i want to generalize above program and size of chx[1...5] and chy[1...5] may increase or decrease depend on user
0 Comments
Accepted Answer
José-Luis
on 7 Oct 2014
And this is a prime example of why you should store your variables in a single matrix instead of with "numbered" names.
So the answer would be:
Place all gudxn and gudyn in a single array and loop through that array instead.
gudx(1) = gudx1;
gudx(2) = gudx2;
Ad nauseam...
4 Comments
José-Luis
on 7 Oct 2014
I'm sorry but I don't follow your logic. Are you testing for all possible permutations of gudx and gudy?
More Answers (0)
See Also
Categories
Find more on Genetic Algorithm in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!