MATLAB: How to compare previous and next values in MATLAB

number_pattern

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..

Best Answer

hi
I believe this works :
function number_patter
b = [];
a = input ('Please input a number: ');
while 1
b = input ('Please input number (I''m looking for a pattern): ');
if a == b
fprintf('That''s it. Well done!\n');
return;
elseif b == prev_b
fprintf('That''s it. Well done!\n');
return;
else
fprintf('Sorry, that''s not what I''m looking for.\n');
end
prev_b = b;
end