Problem with my function

조회 수: 1 (최근 30일)
Febin Benjamin
Febin Benjamin 2014년 7월 9일
댓글: per isakson 2014년 7월 9일
I have a fitness function as shown below.
function z=sample_fit(r)
Flag=0;
while ~Flag
//take inputs from user
Flag=1;
end
//process the inputs
The above function is called for n times by a GA solver. The problem is that, use of flag in the function is useless as it is always set to zero on every iteration. I want the user to be requested for input only once in the first iteration and for rest of the iterations it will use the values provided in the first iteration.

답변 (1개)

per isakson
per isakson 2014년 7월 9일
If in the first iteration the word, iteration, means call of the function, sample_fit, see persistent, Define persistent variable
  댓글 수: 2
Febin Benjamin
Febin Benjamin 2014년 7월 9일
Per isakson, you mean like this?
function z=sample_fit(r)
persistent Flag;
Flag=0;
while ~Flag
//accept inputs from user
Flag=1;
end
//process inputs
per isakson
per isakson 2014년 7월 9일
If you make a simple example to try this construct you will find that it doesn't work. Flag=0 (btw: I prefer Flag=false) will be executed every time; nothing achieved with persitent.
See first example in documentation of persistent. You need something like
if isempty( Flag )
Flag = false;
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by