I want to find 4 consecutive number which is present in array.

6 views (last 30 days)
idx=[1 2 4 5 7 9 11 12 13 14 16]
result should be
ans= 11 12 13 14

Accepted Answer

Davide Masiello
Davide Masiello on 13 Oct 2022
idx = [1 2 4 5 7 9 11 12 13 14 16];
pl = regionprops(any(idx == (1:max(idx))',2),'PixelList');
out = pl(cellfun(@(x)size(x,1),{pl.PixelList}) == 4).PixelList(:,2)'
out = 1×4
11 12 13 14

More Answers (3)

John D'Errico
John D'Errico on 13 Oct 2022
Edited: John D'Errico on 13 Oct 2022
Easy. What property does a sequence of 4 consecutive numbers have? I suppose there may be many such properties, for example, there are many useless properties. For example, if you know that not all four of the numbers in such a consecutve sequence can be divisible by 7, would that help you to identify anything? Not really. So what USEFUL property should you think of?
I'd suggest that the simplest useful property is to look at the consecutive differences, of all numbers in your vector. Thus, if we have idx as:
idx=[1 2 4 5 7 9 11 12 13 14 16];
diffidx = diff(idx)
diffidx = 1×10
1 2 1 2 2 2 1 1 1 2
So what would happen if you applied such a difference operator to ANY sequence of consecutive integers? The answer is simple. You would find that each number in such a sub-sequence is exactly 1 larger than the one immediately before it, so diff MUST then yield a sequence of ones.
Is there any such sequence in the difference vector? (Of course there is, and it could only have arisen from a sequence of 4 consecutuve integers.)
So what matters now, is, can you find a sequence of consecutive ones? As it turns out, strfind can help here, and strfind actually works on a vector of integers too. (There are probably other tools I could use to identify such a sub-sequence of ones too, but strfind is the perfect one here.)
loc = strfind(diffidx,[1 1 1])
loc = 7
So the locatino of a sequence of exactly 3 ones happened once and only once, and it started at location 7. That tells you elements 7:(7+3) were consecutive integers in the original vector.
idx(loc:loc+3)
ans = 1×4
11 12 13 14
Easy peasy, at least when you break the problem down into smaller subproblems.
When you have a difficult problem, think about what properties you can use to your benefit. Think about ways you can use tools in MATLAB that can help you, even if they might not solve your problem completely, can they get you just a little closer to the solution? Maybe you can couple a few things together to give the answer. In this case, the tools diff and strfind were both valuable tools to solve the problem.

David Hill
David Hill on 13 Oct 2022
idx=[1 2 4 5 7 9 11 12 13 14 16];
d=num2str(diff(idx)==1);
d(d==' ')=[];
f=strfind(d,'111');
idx(f:f+3)
ans = 1×4
11 12 13 14
  2 Comments
Jan
Jan on 13 Oct 2022
Edited: Jan on 13 Oct 2022
@David Hill: A conversion to char is not needed:
idx = [1 2 4 5 7 9 11 12 13 14 16];
d = (diff(idx)==1);
f = strfind(d, [1,1,1]); % Operates on row vectors of type double also
idx(f:f+3)
ans = 1×4
11 12 13 14

Sign in to comment.


Umesh Kharad
Umesh Kharad on 13 Oct 2022
idx = [1 2 4 5 7 9 11 12 13 14 16];
for k=1:end
if idx(k:k+3)==(idx(k):idx(k)+3)
fprintf('error in position %g: \n',k);
disp(idx(k:k+3));
end
end

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!