error in generalizing the program
Afficher commentaires plus anciens
hi all
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 and same for chy1.chy2......., means we cannot change a value of above variable twice or thrice.
3rd point is pair of values -1 are not allowed, means both gudx(1.2....6) and gudx(1.2....6) must not be equal to -1. if any one has value -1 then this pair is also not possible
chx=[chx1 chx2 chx3 chx4 chx5 chx6] chy=[chy1 chy2 chy3 chy4 chy5 chy6]
e.g
numChromosomes=6
chx =[20 15 95 85 10 15] chy =[90 90 100 15 90 90]
genx =[20 15 95 85 10 15] geny =[90 90 100 15 90 90]
gudx =[-1 -1 95 85 10 -1] gudy =[90 90 -1 -1 90 -1]
here you can see that pair will be. gudx(1,3)-gudx(1,4), gudx4-gudx5, gudx5-gudx1, and. gudy(1,1)-gudy(1,2), gudy2-gudy5, gudy5-gudy1,
desired output:
chx =[20 15 85 10 95 15] chy =[90 90 100 15 90 90]
following is approximate program of above problem. but it do not provide satisfactory output which i want :( kindly help me, i will be highly thankful to you sir. %x
for u=1:length(numChromosomes)
if u==length(numChromosomes)
strtId = 1;
stopId = u-1;
else
strtId = u+1;
stopId = length(numChromosomes);
end
for v=strtId:stopId
if ((gudx(u)~=1) & (gudx(v)~=-1))
if chx(u)==genx(u)
chx(u) = gudx(v)
end
end
end
end
%y
for u=1:length(numChromosomes)
if u==length(numChromosomes)
strtId = 1;
stopId = u-1;
else
strtId = u+1;
stopId = length(numChromosomes);
end
for v=strtId:stopId
if ((gudy(u)~=1) & (gudy(v)~=-1))
if chy(u)==geny(u)
chy(u) = gudy(v)
end
end
end
end
output:
chx =20 15 95 85 10 15 chy =90 90 100 15 90 90
it return the same values of chx and chy without any change :( , kindly notify where i have to make changes. thanx in advance
Réponse acceptée
Plus de réponses (3)
Mudasir Ahmed
le 16 Oct 2014
0 votes
Mudasir Ahmed
le 17 Oct 2014
1 commentaire
Guillaume
le 17 Oct 2014
5-3 is not allowed according to your first rule. Only 6 is allowed to pair with smaller numbers.
So you either need to reduce your number of chromosome by 1 or state the rule that allows 5-3.
Note that if your reduce the number of chromosomes to 5, you also need to reduce the numbers of elements in chx and gudx to match.
Mudasir Ahmed
le 17 Oct 2014
0 votes
9 commentaires
Guillaume
le 17 Oct 2014
Please, use comment on this Answer just below my comment/answer instead of starting a new answer each time.
Can you rephrase your comment? I'm afraid I don't understand. As I said, which rule would result in 5-3? As per your rules:
- pair with itself and decreasing one is not allowed except the final variable, who can pair with all variable below him except himself. Generates 1-2 1-3 1-4 1-5 1-6 2-3 2-4 2-5 2-6 ... 5-6 6-1 6-2 6-3 6-4 6-5. You'll notice there's no 5-3.
- pair of values -1 are not allowed. Thus only 3-4 3-5 4-5 remain.
- changing of any variable chx1,chx2,chx3,chx4,chx5 is allowed only one time. Thus only 3-4 and 3-5 remain.
If you want 5-3, the rules must be different.
Mudasir Ahmed
le 17 Oct 2014
Guillaume
le 17 Oct 2014
Right, so the initial pairs are not selected depending on the number of chromosomes, but just based on a preselected list. You only need to modify the pair construction.
shortlist = [1 2 3 4 5];
startpairs = nchoosek(shortlist, 2);
endpairs = [repmat(shortlist(end), numel(shortlist)-1, 1) shortlist(1:end-1)'];
%... rest of the code as normal
Mudasir Ahmed
le 17 Oct 2014
Mudasir Ahmed
le 17 Oct 2014
Guillaume
le 17 Oct 2014
I did show you the way to do the swap in my original example. You certainly don't need a loop. it's simply:
chx(allpairsx(:, 1)) = genx(allpairsx(:, 2));
You also don't need a loop to generate your pairx:
pairx = find(gudx ~= -1);
Mudasir Ahmed
le 18 Oct 2014
Guillaume
le 18 Oct 2014
Once again, the answer is only a few short posts up from your question, so I'll just copy-paste:
shortlist = [1 2 3 4 5];
startpairs = nchoosek(shortlist, 2);
endpairs = [repmat(shortlist(end), numel(shortlist)-1, 1) shortlist(1:end-1)'];
Replace shortlist with your pairx, and it'll work.
Mudasir Ahmed
le 19 Oct 2014
Catégories
En savoir plus sur Time-Frequency Analysis dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!