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

Guillaume
Guillaume le 16 Oct 2014
One obvious mistake in the code you've posted is that you're using length(numChromosomes) which, if numChromosomes = 6, is equal to 1. You probably meant to use numChromosomes directly.
Also, it would appear you've made a mistake in your
if ((gudx(u) ~= 1)...
which should be
if ((gudx(u) ~= -1) ... %same for gudy
I don't understand your example in its entirety. For x, why is 5-1 selected, when gudx(1) == -1 and there are 6 elements in gudx. I thought only the last value (6) was allowed to pair with lower numbers.
Nonetheless, the first thing I noticed is that the pairs in your introduction is the output of:
nchoosek(1:5, 2)
plus some additional elements for the end pairs. I would use that to generate your pairs and then eliminate the non-valid ones where gud? == -1 and where a value is repeated. Like this:
That is
startpairs = nchoosek(1:numChromosomes, 2); %build 1-2, 2-3, etc. to 5-6
endpairs = [repmat(numChromosomes, numChromosomes-1 , 1) (1:numChromosomes-1)']; %build 6-1, 6-2, etc. to 6-5
allpairsx = [startpairs; endpairs];
invalid = find(gudx == -1); %pair values that are invalid because gudx == -1
allpairsx(ismember(allpairsx(:, 1), invalid) | ismember(allpairsx(:, 2), invalid), :) = []; %remove those invalid pairs
[~, valid] = unique(allpairsx(:, 1), 'stable'); %remove duplicates of first part of pair
allpairsx = allpairsx(valid, :);
[~, valid] = unique(allpairsx(:, 2), 'stable'); %remove duplicates of second half of pair
allpairsx = allpairsx(valid, :);
%do the swap:
chx(allpairsx(:, 1)) = gudx(allpairsx(:, 2));
%same for y
If it's not exactly what you want, I hope it at least points you in the right direction.

Plus de réponses (3)

Mudasir Ahmed
Mudasir Ahmed le 16 Oct 2014

0 votes

sorry sir, actually i made first for 5 elements then i extend it to 6 , correct sequence is. gudx1-gudx2, 1-3, 1-4, 1-5,1-6, 2-3, 2-4, 2-5,2-6, 3-4, 3-5,3-6, 4-5,4-6, 5-6, 6-1,6-2,6-3,6-4,6-5,
and you are right sir, typing mistake i did, correct is
if ((gudx(u)~=-1) & (gudx(v)~=-1))
Mudasir Ahmed
Mudasir Ahmed le 17 Oct 2014
hi sir,
sir i need your kind help. 80% of your written program working fine and also i understood it too. but still something is missing
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]
startpairs = nchoosek(1:numChromosomes, 2) %build 1-2, 2-3, etc. to 5-6 endpairs = [repmat(numChromosomes, numChromosomes-1 , 1) (1:numChromosomes-1)'] %build 6-1, 6-2, etc. to 6-5 allpairsx = [startpairs; endpairs] invalid = find(gudx == -1) %pair values that are invalid because gudx == -1
allpairsx(ismember(allpairsx(:, 1), invalid) | ismember(allpairsx(:, 2), invalid), :) = [] %remove those invalid pairs [~, valid] = unique(allpairsx(:, 1)) %remove duplicates of first part of pair allpairsx = allpairsx(valid, :)
output upto this stage is... which is also correct
allpairsx =
3 4
4 5
now i want pair 5 with 3. how this will be possible using above technique sir. 3 4 4 5 5 3

1 commentaire

Guillaume
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.

Connectez-vous pour commenter.

Mudasir Ahmed
Mudasir Ahmed le 17 Oct 2014

0 votes

i want to pair which are selected out of 6. 3,4,5 is selected which have value not -1 so now i have to pair 3-4,4-5,5-3

9 commentaires

Guillaume
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:
  1. 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.
  2. pair of values -1 are not allowed. Thus only 3-4 3-5 4-5 remain.
  3. 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
Mudasir Ahmed le 17 Oct 2014
sorry sir, next time i will click on comment on this answer box.
sir, except the final variable means the variable which is in last of shortlisted list of 3 4 5, here 5 is last variable of shortlisted list so now i have to make 3-4, 4-5 ,5-3
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
Sir by adding few changes i have write the folowing program
clear
clc
c=1
numChromosomes=6
chx =[30 50 95 85 10 50]
genx =[30 50 95 85 10 50]
gudx =[-1 -1 95 85 10 -1]
for a=1:numChromosomes
if gudx(1,a)~=-1
pairx(1,c)=a
c=c+1
end
end
startpairs = nchoosek(pairx(1):pairx(c-1), 2)
endpairs = [repmat(pairx(1,c-1), c-2 , 1) (pairx(1,1):pairx(c-2))']
allpairsx=[startpairs;endpairs]
[~, valid] = unique(allpairsx(:, 1), 'stable')
allpairsx = allpairsx(valid, :)
*output upto this stage
allpairsx =
3 4
4 5
5 3*
now i want to exchange the 4th element in chx with 3rd element in chx, and 5th to 4, 3rd to 5, the desired output must be chx=[30 50 85 10 95 50]
i have tried and make the following program, observed the same thing it doesn't work :D
for a=c-1
for b=1:numChromosomes
if allpairsx(a,2)==b
for c=1:numChromosomes
if allpairsx(a,1)==c
chx(1,c)=genx(1,b)
end
end
end
end
end
Mudasir Ahmed
Mudasir Ahmed le 17 Oct 2014
thanks sir i built it.
clear clc c=1
numChromosomes=6
chx =[30 50 95 85 10 50]
genx =[30 50 95 85 10 50]
gudx =[-1 -1 95 85 10 -1]
for a=1:numChromosomes if gudx(1,a)~=-1 pairx(1,c)=a c=c+1 end end
startpairs = nchoosek(pairx(1):pairx(c-1), 2)
endpairs = [repmat(pairx(1,c-1), c-2 , 1) (pairx(1,1):pairx(c-2))']
allpairsx=[startpairs;endpairs]
[~, valid] = unique(allpairsx(:, 1), 'stable')
allpairsx = allpairsx(valid, :)
for a=1:c-1 for b=1:numChromosomes if allpairsx(a,2)==b for d=1:numChromosomes if allpairsx(a,1)==d chx(1,d)=genx(1,b) end end end end end
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);
c=1;
gudx =[85 -1 85 20 95 -1]
for a=1:numChromosomes if gudx(1,a)~=-1 pairx(1,c)=a c=c+1 end end
output
pairx =
1 3 4 5
then
startpairsx = nchoosek(pairx(1):pairx(c-1), 2)
must give the following response 1 3 1 4 1 5 3 4 3 5 4 5
but instead of this matlab gives following response
startpairsx =
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5
and the following line generates error
endpairsx = [repmat(pairx(1,c-1), c-2 , 1) (pairx(1,1):pairx(c-2))']
which is
Error using horzcat Dimensions of matrices being concatenated are not consistent.
sir kindly help me.
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
Mudasir Ahmed le 19 Oct 2014
yes sir, you are right, i was confused :( thanks allot sir :)

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by