How to run loop again with output as input?
조회 수: 2 (최근 30일)
이전 댓글 표시
Hello, I'm having issues with my code below. I'm trying to find a value of m based on a previous value of M. I then calculate another variable fitness based on this value M, and allow the 100 individuals with the highest fitness value to survive. I want their M to be used to repeat the loop, i.e. I want their M to become the new rand(100,1). I tried putting a loop around this but it doesn't work. Does anyone know what is going wrong?
e = 0.5;
% 100 individuals with random m values in [0,1]
M = rand(100,1);
for k = 1:10
for j = 1:100
for i = 1:10
moff(i,j) = M(j,:) + randi([-1,1])*(0.1)*M(j,:); % offspring inherits parents m + noise
end
end
Mreshape = reshape(moff, [1000,1]);
% determine individual T
T = [1 - Mreshape + unifrnd(-e,e)];
% determine S
S = Mreshape;
% determine fitness
F = [T.*S, Mreshape];
% determine fittest individuals
Fsort = sortrows(F, 'descend');
% fittest 100 offspring survive to become parents in next generation
Ffit = Fsort([1:100], :);
% extract m from fittest offspring
M = Fsort(:, 2);
end
댓글 수: 3
Thiago Henrique Gomes Lobato
2020년 2월 23일
No, you actually get the 100 best values, and, in your case, they converge very quickly, so by 10 iterations they are all the same, which is not actually an error but rather an expected behaviour of a convergent optimization. If you do only two iterations for k (for k=1:2) your M will have different, but very similar, values.
답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!