nested loop in eval command and generalized the programme
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Mudasir Ahmed
le 9 Oct 2014
Commenté : Mudasir Ahmed
le 10 Oct 2014
hi all
i want to generalized the following program. the theme is...actually i want to pair in the following manner, gudx1-gudx2, 1-3, 1-4, 1-5, 2-3, 2-4, 2-5, 3-4, 3-5, 4-5, 5-1, 5-2, 5-3, 5-4
pair with itself and decreasing one is not allowed except the final variable, who can pair with all variable below him except himself.
and 2nd main point is changing of any variable chx1,chx2,chx3,chx4,chx5 is allowed only one time, means we cannot change a value of above variable twice or thrice.
chx1=genx1
chx2=genx2
chx3=genx3
chx4=genx4
chx5=genx5
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
i have tried but i know this one is logically and syntax wise is wrong.
for i:1:numChromosomes
for j=i+1:numChromosomes
eval(sprintf('if ((gudx%d>0) & (gudx%d>0))',[i j],'if(chx%d==gen%d)',[i i],'chx%d==gud%d',[i j],end,end))
end
end
kindly help me too generalize it i will be highly thankful to you
regards
0 commentaires
Réponse acceptée
Geoff Hayes
le 9 Oct 2014
Mudasir - you should consider using matrices to manage your chromosome data, rather than creating individual variables. If we assume that your gudx, genx, and chx variables are integers, then we could do something like the following
chx = [chx1 ; chx2 ; chx3 ; chx4 ; chx5];
gudx = [gudx1 ; gudx2 ; gudx3 ; gudx4 ; gudx5];
genx = [genx1 ; genx2 ; genx3 ; genx4 ; genx5];
for u=1:length(gudx)
% handle the case where if u is last element, then we compare
% against all elements
if u==length(gudx)
strtId = 1;
stopId = u-1;
else
strtId = u+1;
stopId = length(gudx);
end
for v=strtId:stopId
if gudx(u)>0 && gudx(v)>0
if chx(u)==genx(u)
chx(u) = gudx(v);
% since chx(u) has changed, then exit the inner
% for loop
break;
end
end
end
end
The above code runs, but you should ensure that all conditions that you require are met.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Solver Outputs and Iterative Display dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!