How to tell if there are at least 5 consecutive entries in one 8-by-1 matrix with 8 integers?
古いコメントを表示
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 件のコメント
Daniel Shub
2013 年 5 月 31 日
First off, this sounds like homework. What have you tried so far? Can you give examples of what should and should not pass your test. Does [5,4,3,2,1,10,20,30] pass? What about [1,3,2,4,5,10,20,30]?
Daniel Shub
2013 年 5 月 31 日
This sounds to me like a good cody problem, although I really don't know what makes a good cody problem, there are so many ways to do it optimizing it might be fun.
Azzi Abdelmalek
2013 年 5 月 31 日
Han, it's better if you explain with a numeric example
Azzi Abdelmalek
2013 年 5 月 31 日
編集済み: Azzi Abdelmalek
2013 年 5 月 31 日
Can you explain, in the two cases, what are your consecutive numbers? what means "consecutive in straight"
Han
2013 年 5 月 31 日
Daniel Shub
2013 年 5 月 31 日
Consider a standard 52 card deck of playing cards where the face cards are represented by 11, 12, and 13. For a 5 card hand, you have a straight if your sorted hand is equal to a:(a+5) for any a. For an 8 card hand you have a 5 card straight if any 5 card subset of your sorted hand is equal to a:(a+5).
Han
2013 年 5 月 31 日
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
2013 年 6 月 1 日
採用された回答
その他の回答 (4 件)
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)
1 件のコメント
Daniel Shub
2013 年 6 月 1 日
Looking at the non sequential entries, nice.
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
2013 年 5 月 31 日
That says that [1; 2; 3; 5; 6; 7; 9; 10] has at least 5 consecutive entries. Not how I would have defined 5 consecutive, but it might be what the OP is looking for.
Azzi Abdelmalek
2013 年 5 月 31 日
what about this example?
data = [0; 1; 2; 3; 1; 5; 6; 9] % Sample data.
Image Analyst
2013 年 5 月 31 日
編集済み: Image Analyst
2013 年 5 月 31 日
I assumed integers increasing by one, but I agree that there could be other interpretations of what consecutive means and he should clarify that. Maybe he did when he commented to Azzi "I need at least 5 consecutive values in a "straight". "
I think the best way would be to use bwlabel() in the Image Processing Toolbox followed by regionprops to measure the lengths, but I don't know if he has the Image Processing Toolbox. If you do Han, let me know and there is a simple code that can do it robustly:
data = [9; 1; 2; 3; 4; 5; 6; 9]
% data = [1; 2; 3; 5; 6; 7; 9; 10]
diffData = diff(data)
labeled = bwlabel(diffData==1)
% Make measurements of all the stretches of numbers increasing by 1
measurements = regionprops(labeled, 'area');
AllLengths = [measurements.Area]
% See if any are more than 5
hasAtLeastOne5 = any(AllLengths > 5)
Han
2013 年 5 月 31 日
Daniel Shub
2013 年 5 月 31 日
@Han Azzi example has two straights/runs 0, 1, 2, 3 and 5, 6.
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
2013 年 5 月 31 日
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 件のコメント
Han
2013 年 5 月 31 日
Daniel Shub
2013 年 5 月 31 日
I am confused. When you do e(e==0)=1 you set all the false values equal to true such that all(e) will always return true. You then go looking for a run of trues in a vector of trues.
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
2013 年 5 月 31 日
Of course, apparently I am not thinking straight, but a = ones(1, 8) still passes your test.
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
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
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)
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!