How to run loop again with output as input?
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Ellen Brown
el 23 de Feb. de 2020
Comentada: Thiago Henrique Gomes Lobato
el 23 de Feb. de 2020
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 comentarios
Thiago Henrique Gomes Lobato
el 23 de Feb. de 2020
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.
Respuestas (0)
Ver también
Categorías
Más información sobre Industrial Statistics en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!