error in generalizing the program

1 view (last 30 days)
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

Accepted Answer

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

More Answers (3)

Mudasir Ahmed
Mudasir Ahmed on 16 Oct 2014
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 on 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 Comment
Guillaume
Guillaume on 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.

Sign in to comment.


Mudasir Ahmed
Mudasir Ahmed on 17 Oct 2014
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 Comments
Guillaume
Guillaume on 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
Mudasir Ahmed on 19 Oct 2014
yes sir, you are right, i was confused :( thanks allot sir :)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!