How to tell if there are at least 5 consecutive entries in one 8-by-1 matrix with 8 integers?

1 回表示 (過去 30 日間)
Hi:
I am looking for a way to determine if there are AT LEAST 5 consecutive values in one 8-by-1 matrix with 8 integers. The 8 integers do NOT have to be unique. And I prefer this to be short and loop-free.
The consecutive values have to be in a "straight", meaning that [5 1 3 4 2 6 8 7] would work because it has at least 5 (actually 8) consecutive values in a straight. The straight is 1 2 3 4 5 6 7 8.
But [1 2 3 4 10 9 8 7] would not because it only has 4 values in each of the straight. The first straight is 1 2 3 4. The second straight is 7 8 9 10.
Thank you very much for your help (I'm writing this for my TexasHoldEm function for fun)
  10 件のコメント
Image Analyst
Image Analyst 2013 年 5 月 31 日
[5 1 3 4 2 6 8 7] is 1 by 8, not 8 by 1 like you said, and my code assumes. Which is it??? [5; 1; 3; 4; 2; 6; 8; 7] would be 8 by 1.
Han
Han 2013 年 6 月 1 日
@Image Analyst: You are right, it should be [5 1 3 4 2 6 8 7]'

サインインしてコメントする。

採用された回答

Daniel Shub
Daniel Shub 2013 年 5 月 31 日
編集済み: Daniel Shub 2013 年 6 月 3 日
As people are giving answers, I am pretty confident that
not(isempty(strfind(diff(sort(unique(x))), ones(1, 4))))
works. I am less confident that
not(all(diff(sort(unique(x)), 4)))
works, but if it does, it is much cooler as I rarely use the second argument to diff. After further thinking the second approach does not work. It fails in all sorts of unique ways, for example 1,3,5,7,9.
  3 件のコメント
Jan
Jan 2013 年 5 月 31 日
編集済み: Jan 2013 年 5 月 31 日
You can replace not(isempty(strfind())) by any(strfind())
And the output of unique is still sorted. Then your first method becomes:
any(strfind(diff(unique(x)), ones(1, 4)))
Han
Han 2013 年 6 月 1 日
any(strfind(diff(unique(x)), ones(1, 4))) it is then. Thanks

サインインしてコメントする。

その他の回答 (4 件)

Roger Stafford
Roger Stafford 2013 年 5 月 31 日
編集済み: Roger Stafford 2013 年 6 月 1 日
It can all be put into one line:
any(diff(find([true;diff(unique(x))~=1;true]))>=5)

Image Analyst
Image Analyst 2013 年 5 月 31 日
How about
data = [9; 1; 2; 3; 4; 5; 6; 9] % Sample data.
diffData = diff(data)
countOf1s = sum(diffData==1)+1
atLeast5 = countOf1s >= 5
  7 件のコメント
Daniel Shub
Daniel Shub 2013 年 5 月 31 日
@IA I would be surprised if bwlabel followed by regionprops would win Cody or any type of speed test.
Han
Han 2013 年 5 月 31 日
@Image Analyst Thanks very much for your help. But I do not have image processing toolbox. I heard it's expensive.

サインインしてコメントする。


Azzi Abdelmalek
Azzi Abdelmalek 2013 年 5 月 31 日
a=[3 2 3 4 5 5 7 6 8];
e=[1 diff(a)];
e(e==0)=1;
idx=strfind(e,[true,true,true,true]) % it exist if idx~=0
  4 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 5 月 31 日
編集済み: Azzi Abdelmalek 2013 年 5 月 31 日
diff(a) can have several values, 0,-1,1,2,....I have grouped 1 and 0
Daniel Shub
Daniel Shub 2013 年 5 月 31 日
Of course, apparently I am not thinking straight, but a = ones(1, 8) still passes your test.

サインインしてコメントする。


Azzi Abdelmalek
Azzi Abdelmalek 2013 年 5 月 31 日
編集済み: Azzi Abdelmalek 2013 年 5 月 31 日
a=[1; 2; 3; 5; 6; 7; 9; 10]
a=sort(a)
e=[1 ;diff(a)];
e(e==0)=1;
idx=~isempty(strfind(e',[true,true,true,true])) % it exist if idx=1
  2 件のコメント
Daniel Shub
Daniel Shub 2013 年 5 月 31 日
This code doesn't work you need: e=[1 , diff(a)];.
Second, it says a = ones(1, 8) is a straight.
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 5 月 31 日
編集済み: Azzi Abdelmalek 2013 年 5 月 31 日
Yes, Look at edited answer (e' instead of e), and this is working for a=ones(8,1)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGraphics Object Programming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by