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

12 次查看(过去 30 天)
idx=[1 2 4 5 7 9 11 12 13 14 16]
result should be
ans= 11 12 13 14

采纳的回答

Davide Masiello
Davide Masiello 2022-10-13
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

更多回答(3 个)

John D'Errico
John D'Errico 2022-10-13
编辑:John D'Errico 2022-10-13
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 2022-10-13
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 个评论
Jan
Jan 2022-10-13
编辑:Jan 2022-10-13
@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

请先登录,再进行评论。


Umesh Kharad
Umesh Kharad 2022-10-13
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

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by