how to compare previous and next values in MATLAB?
Mostrar comentarios más antiguos
Write a function called number_pattern that takes no input arguments and returns no output arguments. Instead it gets it input via the function input. It asks for a number repeatedly until the user puts in the same number twice in a row. The loop that it uses must be a while-loop. Here is an example run:
>> matching_number
Please input a number: 4
Please input number (I'm looking for a pattern): 5
Sorry, that's not what I'm looking for.
Please input number (I'm looking for a pattern): 6
Sorry, that's not what I'm looking for.
Please input number (I'm looking for a pattern): 7
Sorry, that's not what I'm looking for.
Please input number (I'm looking for a pattern): 7
That's it. Well done!
The function should behave just as in the example, using exactly the same phrasing in its output (e.g., “That’s it. Well done!”)
I tried to write code but I did not understand how can I compare previous "b" and next "b". Here is my code..

4 comentarios
Turlough Hughes
el 6 de Nov. de 2020
I would treat a as being the last value (inputted by the user) and b as the current value. Following user input, you can check if a == b and following that you need to update a for the next iteration so that it is equal to b from the current one.
Mathieu NOE
el 6 de Nov. de 2020
yeap
if you share you code, we can further help you
zehra ülgen
el 6 de Nov. de 2020
zehra ülgen
el 6 de Nov. de 2020
Respuesta aceptada
Más respuestas (1)
Jon
el 6 de Nov. de 2020
You need to create a new variable, call it for example prevB. After each loop set
prevB = B
when you do your comparison compare prevB and B
Note you will need to initialize prevB before entering the loop so it is defined the first time through. Just set it to something that the user would never enter for example prevB = inf
2 comentarios
zehra ülgen
el 6 de Nov. de 2020
Jon
el 9 de Nov. de 2020
You need to assign prevB = b insinde of the loop
Categorías
Más información sobre Dates and Time en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!