how to compare previous and next values in MATLAB?

20 次查看(过去 30 天)
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 个评论
zehra ülgen
zehra ülgen 2020-11-6
function number_patter
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 == b
fprintf('That''s it. Well done!\n');
return;
else
fprintf('Sorry, that''s not what I''m looking for.\n');
end
end
zehra ülgen
zehra ülgen 2020-11-6
'number repeatedly until the user puts in the same number twice in a row'
to do that statement I should compare the previous 'b value' and new 'b value'. But when I said b == b my code is not working. How can I compare last two b value?

请先登录,再进行评论。

采纳的回答

Mathieu NOE
Mathieu NOE 2020-11-7
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
  2 个评论
zehra ülgen
zehra ülgen 2020-11-8
I'm sorry it's nor working..
>> number_pattern
Please input a number: 4
Please input number (I'm looking for a pattern): 5
Unrecognized function or variable 'prev_b'.
Error in number_pattern (line 9)
elseif b == prev_b
But if I change the prev_b = b; like that.. Code is working.. Thank you very much for your helps..
function number_patter
b = [];
a = input ('Please input a number: ');
while 1
prev_b = b;
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
end

请先登录,再进行评论。

更多回答(1 个)

Jon
Jon 2020-11-6
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 个评论
zehra ülgen
zehra ülgen 2020-11-6
I'm not sure I got you right but I tried to do something and it is not working again..
function number_patter
a = input ('Please input a number: ');
prevB = input ('Please input number (I''m looking for a pattern): ');
while 1
b = input ('Please input number (I''m looking for a pattern): ');
if b == a
fprintf('That''s it. Well done!\n');
break;
elseif b == prevB
fprintf('That''s it. Well done!\n');
break;
else
fprintf('Sorry, that''s not what I''m looking for.\n');
end
end
Jon
Jon 2020-11-9
You need to assign prevB = b insinde of the loop

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by