How to tell if there are at least 5 consecutive entries in one 8-by-1 matrix with 8 integers?
Show older comments
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
Daniel Shub
on 31 May 2013
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
on 31 May 2013
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
on 31 May 2013
Han, it's better if you explain with a numeric example
Azzi Abdelmalek
on 31 May 2013
Edited: Azzi Abdelmalek
on 31 May 2013
Can you explain, in the two cases, what are your consecutive numbers? what means "consecutive in straight"
Han
on 31 May 2013
Daniel Shub
on 31 May 2013
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
on 31 May 2013
Image Analyst
on 31 May 2013
[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
on 1 Jun 2013
Accepted Answer
More Answers (4)
Roger Stafford
on 31 May 2013
Edited: Roger Stafford
on 1 Jun 2013
It can all be put into one line:
any(diff(find([true;diff(unique(x))~=1;true]))>=5)
1 Comment
Daniel Shub
on 1 Jun 2013
Looking at the non sequential entries, nice.
Image Analyst
on 31 May 2013
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
Daniel Shub
on 31 May 2013
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
on 31 May 2013
what about this example?
data = [0; 1; 2; 3; 1; 5; 6; 9] % Sample data.
Image Analyst
on 31 May 2013
Edited: Image Analyst
on 31 May 2013
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
on 31 May 2013
Daniel Shub
on 31 May 2013
@Han Azzi example has two straights/runs 0, 1, 2, 3 and 5, 6.
Daniel Shub
on 31 May 2013
@IA I would be surprised if bwlabel followed by regionprops would win Cody or any type of speed test.
Han
on 31 May 2013
Azzi Abdelmalek
on 31 May 2013
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
Han
on 31 May 2013
Daniel Shub
on 31 May 2013
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
on 31 May 2013
Edited: Azzi Abdelmalek
on 31 May 2013
diff(a) can have several values, 0,-1,1,2,....I have grouped 1 and 0
Daniel Shub
on 31 May 2013
Of course, apparently I am not thinking straight, but a = ones(1, 8) still passes your test.
Azzi Abdelmalek
on 31 May 2013
Edited: Azzi Abdelmalek
on 31 May 2013
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
Daniel Shub
on 31 May 2013
This code doesn't work you need: e=[1 , diff(a)];.
Second, it says a = ones(1, 8) is a straight.
Azzi Abdelmalek
on 31 May 2013
Edited: Azzi Abdelmalek
on 31 May 2013
Yes, Look at edited answer (e' instead of e), and this is working for a=ones(8,1)
Categories
Find more on Rubik's Cube in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!