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 Comments

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]?
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.
Han, it's better if you explain with a numeric example
@Azzi: Ok, so [5 1 3 4 2 6 8 7] would work since it has at least 5 (actually 8) consecutive values in a "straight". But something like [1 2 3 4 10 9 8 7] would not work because the longest consecutive value set has only 4 values. Does that answer your question Azzi?
Can you explain, in the two cases, what are your consecutive numbers? what means "consecutive in straight"
"straight" is a poker term. A straight consists of 5 values. The first value: p (a random integer). Second value: p+1. The third value: p+2. The fourth value: p+3. The fifth value: p+4
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).
@Azzi: The straight in [5 1 3 4 2 6 8 7] is 1 2 3 4 5 6 7 8. There are two straights in [1 2 3 4 10 9 8 7], the first one is 1 2 3 4. The second one is 7 8 9 10. Since each straight in the second scenario has a length of 4. It does not qualify the condition I made that each straight has to be at least 5-member long. (but the first one does since it has 8 values)
[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.
@Image Analyst: You are right, it should be [5 1 3 4 2 6 8 7]'

Sign in to comment.

 Accepted Answer

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 Comments

@Daniel. Thanks, I'll try it in the morning. It's 1am here right now.
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)))
any(strfind(diff(unique(x)), ones(1, 4))) it is then. Thanks

Sign in to comment.

More Answers (4)

It can all be put into one line:
any(diff(find([true;diff(unique(x))~=1;true]))>=5)
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 Comments

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.
what about this example?
data = [0; 1; 2; 3; 1; 5; 6; 9] % Sample data.
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)
@Azzi: your sample data contains only one straight, it's 0 1 2 3. It has 4 values so it does not satisfy the condition I made that requires at least 5 values.
@Han Azzi example has two straights/runs 0, 1, 2, 3 and 5, 6.
@IA I would be surprised if bwlabel followed by regionprops would win Cody or any type of speed test.
@Image Analyst Thanks very much for your help. But I do not have image processing toolbox. I heard it's expensive.

Sign in to comment.

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 Comments

Sorry for the confusion I put in my problem. I need at least 5 consecutive values in a "straight". I guess I forgot to mention that.
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.
diff(a) can have several values, 0,-1,1,2,....I have grouped 1 and 0
Of course, apparently I am not thinking straight, but a = ones(1, 8) still passes your test.

Sign in to comment.

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 Comments

This code doesn't work you need: e=[1 , diff(a)];.
Second, it says a = ones(1, 8) is a straight.
Yes, Look at edited answer (e' instead of e), and this is working for a=ones(8,1)

Sign in to comment.

Categories

Find more on Rubik's Cube in Help Center and File Exchange

Asked:

Han
on 31 May 2013

Community Treasure Hunt

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

Start Hunting!