필터 지우기
필터 지우기

How can I tell matlab to go back and recalculate?

조회 수: 3 (최근 30일)
MC3105
MC3105 2014년 11월 17일
댓글: per isakson 2014년 11월 17일
Hey everyone,
I am wondering what tricks I can use to tell matlab to go back to a certain point in my code?
I am doing a calculation that uses random numbers (normrnd(0,1)). At the end of the calculation I compare the result with a certain value. If my result is too small or too large, I need to recalculate, using different random numbers. So I want to tell Matlab to go back and start over...
Do you have any ideas how to do this? As I am still pretty new to Matlab, please tell me the easiest way, not the most beautiful one :)
Thanks!!
  댓글 수: 1
per isakson
per isakson 2014년 11월 17일
The answer is the same for most languages: &nbsp while, ..., end

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

채택된 답변

Giorgos Papakonstantinou
Giorgos Papakonstantinou 2014년 11월 17일
Maybe with a while loop. Let's say that you want to recalculate if your result is 10^-2 grater/smaller from your certain value.
So you would define a tolerance:
tol = 1e-2; % your tolerance
myvalue = 10; % you certain value
result = 0; % initialize result
calc = 100; % initialize calc, which is the difference between the result and your certain value
while calc>tol % your condition
% write here all you code
result = rand(1)*10;
calc = abs(result-myvalue); % update at each iteration calc
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by