if command for finding more than three identical numbers in a variable

hi, I need an if condition with the following command. if there are more than three consecutive identical numbers in this matrix then give me an ok signal if not then give me a false signal.
example:
v=[8,8,8,9,5,5,8,8,1];
if there are more than 3 consecutive identical numbers in v (8,8,8) then give me an ok signal otherwise give me a false signal
How can I translate the above command into a matlab command. any kind is appreciated. thanks

1 件のコメント

the cyclist
the cyclist 2015 年 4 月 29 日
Do you need 3 or more consecutive, or more than 3 consecutive?

回答 (3 件)

the cyclist
the cyclist 2015 年 4 月 29 日
編集済み: the cyclist 2015 年 4 月 29 日
You could download runlength from the File Exchange, and then do
max(runlength(v,Inf))>3
Other syntaxes with that function will also give the correct answer.
If you have a very large vector, or many of them, you might consider getting Jan Simon's RunLength instead.
Guillaume
Guillaume 2015 年 4 月 29 日
You can use strfind (which despite the name also works with numbers) to find a [0 0] sequence in the diff of your vector (diff is [0 0] when three consecutive numbers are identical):
oksignal = ~isempty(strfind(diff(v), [0 0]))
Roger Stafford
Roger Stafford 2015 年 4 月 29 日
編集済み: Roger Stafford 2015 年 4 月 29 日
You can devise your own test:
if any(diff([0,find(diff(v)~=0),size(v,2)])>3)
...

この質問は閉じられています。

質問済み:

AA
2015 年 4 月 29 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by