매트랩에서 조건이 만족하지 않으면 다시 처음부터 하게 끔 어떻게 하나요?

13 views (last 30 days)
Ji-hwan Hwang
Ji-hwan Hwang on 24 Nov 2015
Commented: Ji-hwan Hwang on 27 Nov 2015
GA툴을 이용하여 최적값을 구하고자하는데 매트랩에서 조건이 만족하지 않으면 다시 처음부터 반복하게 끔 하고 싶습니다.
예를들어 행렬
A = rand(1,10)>0.5
B = [1 0 0 1 0 0 1 1 1 0]
C = zeros(1,10)
D = 0
for i=1:10
if A(1,i) == B(1,i)
C(1,i) = 2
elseif A(1,i)~=B(1,i)
C(1,i) = B(1,i)
end
for j = 1:10
if C(1,j)>=2
D=D+1
end
end
여기서가 문제인데 만약 D값이 4를 안넘으면 다시 처음부터 4를 넘을때까지 반복시키고 싶습니다. 이부분을 구현을 못하고 있는데 조언을 부탁드립니다.
  2 Comments
Walter Roberson
Walter Roberson on 24 Nov 2015
Approximate translation:
I would like to repeat it off again from the beginning if the conditions are not satisfied in MATLAB to obtain the best value for using the GA tool.
For example, the matrix
A = rand (1,10)> 0.5
B = [1 0 0 1 0 0 1 1 1 0]
C = zeros (1,10)
D = 0
for i = 1: 10
if A (1, i) == B (1, i)
C (1, i) = 2
elseif A (1, i) ~ = B (1, i)
C (1, i) = B (1, i)
end
for j = 1:10
if C (1, j)> = 2
D = D + 1
end
end
If 'If the D value is, where I would like to repeat again from the beginning until it exceeds 4 should not exceed the 4.
There unable to implement this section ask for advice.
Ji-hwan Hwang
Ji-hwan Hwang on 27 Nov 2015
thank you !

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 24 Nov 2015
while true
... do your code ...
if D <= 4
break
end
%need to do it again if D exceeds 4
end
Note: your code is missing an 'end' for the 'for i'. I suspect it should go before the 'for j' but I am not certain of that.

Community Treasure Hunt

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

Start Hunting!