Skipping input prompt before loop
조회 수: 3 (최근 30일)
이전 댓글 표시
I am new to this, so Im sorry if this is an excessively mundane question, but Im just a little confused. I have a script I need to execute with a user-defined starting number, but matlab skips my input prompt entirely and then errors, presumably because there's no set value for my variable because I intended for it to be defined in the prompt.
x=input('Choose positive number')
while x>1;
if rem(x,2)=0
x/2=x;
elseif rem(x,2)=1
3x+1=x;
end
end
Why does matlab skip my input prompt here?
댓글 수: 0
답변 (3개)
Azzi Abdelmalek
2014년 2월 22일
First error: if rem(x,2)=0 (use ==)
second error: x/2=x; what is this?
댓글 수: 0
Jan
2014년 2월 22일
편집: Jan
2014년 2월 22일
The code is almost correct:
x = input('Choose positive number')
while x > 1
if rem(x,2) == 0 % Compare with "=="
x = x / 2; % Move assigned variable to the left
else % Not needed: if rem(x,2)=1
x = 3*x + 1; % "3x" is not recognized as multiplication
end
end
댓글 수: 0
Kyle
2014년 2월 22일
편집: Kyle
2014년 2월 22일
댓글 수: 2
Image Analyst
2014년 2월 22일
A steep learning curve is great and very much desired. If you plot "amount learned" as a function of time, don't you want a very steep curve? It's good that you have a steep learning curve with MATLAB.
Jan
2014년 2월 22일
@Kyle: Please post comments to answers in the corresponding comment section, not as a new answer.
The assignment operator = assigns the result of the expression on the right side to the variable on the left side. Therefore x/2=x is not valid, because there is an expression on the left side.
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!