0

I'm doing a genetic algorithm and I'm testing out how I might create offspring from R, with each row of R corresponding to a parent and each column corresponding to a trait. In my code, I am attempting to mate parents 1 and 2, and parents 3 and 4 to give a total of two children. However, when I run the code, it inserts an extra row of zeros in between child 1 and child 2. Why is this happening?

R=[1,2,3;4,5,6;1000,2000,3000;4000,5000,6000]
for parent=1:2:3
    theta=rand(1);
    trait1=theta*R(0+parent,1)+(1-theta)*R(1+parent,1);
    theta=rand(1);
    trait2=theta*R(0+parent,2)+(1-theta)*R(1+parent,2);
    theta=rand(1);
    trait3=theta*R(0+parent,3)+(1-theta)*R(1+parent,3);

    children(parent,:)=[trait1,trait2,trait3];
end
children

Output:

R =

           1           2           3
           4           5           6
        1000        2000        3000
        4000        5000        6000


children =

   3.0837e+00   4.2959e+00   3.2356e+00
            0            0            0
   2.7330e+03   2.7728e+03   3.0762e+03

Thank you

1 Answers1

1

Your parent variable equals 1 on the first step on cycle and equals 3 on the second step. So you have rows 1 and 3 filled. Add another iteration variable to save results in children or just append rows like that:

R=[1,2,3;4,5,6;1000,2000,3000;4000,5000,6000]
children = [];
for parent=1:2:3
    theta=rand(1);
    trait1=theta*R(0+parent,1)+(1-theta)*R(1+parent,1);
    theta=rand(1);
    trait2=theta*R(0+parent,2)+(1-theta)*R(1+parent,2);
    theta=rand(1);
    trait3=theta*R(0+parent,3)+(1-theta)*R(1+parent,3);

    children=[children;trait1,trait2,trait3];
end
children

Another option with predefined size of array and iteration variable:

R=[1,2,3;4,5,6;1000,2000,3000;4000,5000,6000]
children = zeros (2,3);
i = 1;
for parent=1:2:3
    theta=rand(1);
    trait1=theta*R(0+parent,1)+(1-theta)*R(1+parent,1);
    theta=rand(1);
    trait2=theta*R(0+parent,2)+(1-theta)*R(1+parent,2);
    theta=rand(1);
    trait3=theta*R(0+parent,3)+(1-theta)*R(1+parent,3);

    children(i,:)=[trait1,trait2,trait3];
    i = i + 1;

end
children
Pavel Oganesyan
  • 6,774
  • 4
  • 46
  • 84
  • Appending is never a good idea if you are going to do it continuously – Sardar Usama Oct 29 '17 at 08:20
  • @SardarUsama Do we speak about optimization? `children` was changing size on each iteration in initial code. But i agree, in any case where size of array is known, it is better to define it explicitly. Just not sure why it is important in this topic with 2 rows. Anyway, updated my answer. – Pavel Oganesyan Oct 29 '17 at 08:27
  • A question is meant to be an MCVE. – Sardar Usama Oct 29 '17 at 08:46
  • @SardarUsama Thanks for you attention. MCVE (minimal. complete, verifiable example) has nothing to do with optimization. There are no imaginary numbers in the example. I suppose that using `i` as iteration variable in examples is readable and useful for understanding. – Pavel Oganesyan Oct 29 '17 at 09:01
  • An MCVE is a small example of an actual problem. Providing a code using bad practices assuming that the OP is dealing with a small problem is never a good assumption. – Sardar Usama Oct 29 '17 at 09:03
  • Thank you, I went with the second example and that fixed my problem. Could you please explain the purpose of using children=zeros(2,3) and adding in the variable 'i'? If I remove children=zeros(2,3) but keeping the variable iteration variable "i", my program will still give the desired output. – matlabconqueso Oct 29 '17 at 16:46
  • @matlabconqueso Read [*Preallocating-Arrays*](https://www.mathworks.com/help/matlab/matlab_prog/preallocating-arrays.html) and the purpose of `i` is to store the values at correct row subscript. But rename 'i' to something else. Read why: https://stackoverflow.com/questions/14790740/using-i-and-j-as-variables-in-matlab – Sardar Usama Oct 29 '17 at 17:40