How to run loop again with output as input?

5 次查看(过去 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 个评论
Ellen Brown
Ellen Brown 2020-2-23
That still gives me the same issue :(
When I run that after one loop it gives me the same value for M, rather than 100 independent ones
Thiago Henrique Gomes Lobato
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!

Translated by