Skipping input prompt before loop

3 visualizaciones (últimos 30 días)
Kyle
Kyle el 22 de Feb. de 2014
Comentada: Jan el 22 de Feb. de 2014
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?

Respuestas (3)

Azzi Abdelmalek
Azzi Abdelmalek el 22 de Feb. de 2014
First error: if rem(x,2)=0 (use ==)
second error: x/2=x; what is this?

Jan
Jan el 22 de Feb. de 2014
Editada: Jan el 22 de Feb. de 2014
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 el 22 de Feb. de 2014
Editada: Kyle el 22 de Feb. de 2014
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 comentarios
Image Analyst
Image Analyst el 22 de Feb. de 2014
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 el 22 de Feb. de 2014
@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.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by