I want to find 4 consecutive number which is present in array.
6 views (last 30 days)
Show older comments
idx=[1 2 4 5 7 9 11 12 13 14 16]
result should be
ans= 11 12 13 14
0 Comments
Accepted Answer
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)'
More Answers (3)
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)
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])
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)
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.
0 Comments
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)
2 Comments
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)
Umesh Kharad
on 13 Oct 2022
1 Comment
Jan
on 1 Nov 2022
This does not run:
for k=1:end
Matlab cannot guess, what you call "end".
See Also
Categories
Find more on Logical 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!