Skipping input prompt before loop

조회 수: 3 (최근 30일)
Kyle
Kyle 2014년 2월 22일
댓글: Jan 2014년 2월 22일
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?

답변 (3개)

Azzi Abdelmalek
Azzi Abdelmalek 2014년 2월 22일
First error: if rem(x,2)=0 (use ==)
second error: x/2=x; what is this?

Jan
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

Kyle
Kyle 2014년 2월 22일
편집: Kyle 2014년 2월 22일
Ugh... I can't beleive I forgot to use the *. I'm pretty new to this. I hadn't really gotten to that part yet, since it wont even prompt be for an initial value.
This is for a class... and so far I've been doing fine, but this stuff doesn't come naturally to me so the learning curve is a little steep and every time I step away from it a few days I forget nagging small issues.
Anyway, the basic idea of the script is if it's an even number I need to divide it by two then repeat the process. If it's odd, I need to multiply it by 3 and add 1 and iterate until I get 1 out of it. But that's my intention with this.
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
This is one of the things that confuses me a little bit, you switched x/2=x to x=x/2 . Is this just convention? Or is this important for the program? Like I said, I need the program to divide the number by two if it's an even number and then use the new number in the next iteration of the loop. Intuitively I feel like I need to write it in "chronological" order, saying to divide x/2 and then set taht equal to the next x.
I've always had a logical disconnect with how these loops work when you end up having things like a=a-1 to restart the loop as in my last project. So sometimes it takes me a little while to figure this stuff out. But thank you guys for the help. Im a terrible coder at the moment but I do find this stuff quite enjoyable.
  댓글 수: 2
Image Analyst
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
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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by