How to run loop again with output as input?

조회 수: 1 (최근 30일)
Ellen Brown
Ellen Brown 2020년 2월 23일
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
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개)

카테고리

Help CenterFile Exchange에서 Industrial Statistics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by