Find a series of consecutive numbers in a vector

112 ビュー (過去 30 日間)
Julia
Julia 2013 年 9 月 5 日
Hello, I have a small problem I am trying to solve on Matlab, but I am a stuck.
I have a vector containing timestamps: [34 35 36 78 79 80 81 82 84 85 86 102 103 104 105 106 107 201 202 203 204 ...]
And I would like to find the timestamp which is followed by at least 5 consecutive numbers after it. So in my example, the answer would be 102, because it is the first number which is followed by 5 consecutive numbers.
I tried many things using diff(), but I cannot find a simple way to get that result.
If anyone can help, it would be greatly appreciated.
Thank you!

採用された回答

Roger Stafford
Roger Stafford 2013 年 9 月 6 日
Yet another method. Let t be your timestamp row vector.
N = 5; % Required number of consecutive numbers following a first one
x = diff(t)==1;
f = find([false,x]~=[x,false]);
g = find(f(2:2:end)-f(1:2:end-1)>=N,1,'first');
first_t = t(f(2*g-1)); % First t followed by >=N consecutive numbers
  2 件のコメント
Caixia Liu
Caixia Liu 2018 年 4 月 10 日
編集済み: Caixia Liu 2018 年 4 月 10 日
Thanks for your codes. But it fails when the first index is actually the result.
Nurul Ain Basirah Zakaria
Nurul Ain Basirah Zakaria 2021 年 6 月 12 日
hi, what if, i want something like this:
to find the consecutive value below -1
2.0 2.5 2.1 1.3 1.4 -1.0 -2.1 -1.2 -1.5 -2.1 2.0 3.2 3.0 -1.0 -4.0 -2.1 -1.45 -1.20 -2.0 3.0 2.5 1.2
the first consecutive negative value is categorize as first event and second consecutive negative value is the second event,
then i want to calculate the mean value for each event;
sum of -1.0 -2.1 -1.2 -1.5 -2.1, divide by 5, as there are 5 num of -ve value for first event,
and the second event,
sum of -1.0 -4.0 -2.1 -1.45 -1.20 -2.0, divide by 6, as the are 6 num of -ve value for second event
can i do this in matlab?

サインインしてコメントする。

その他の回答 (6 件)

Laurent
Laurent 2013 年 9 月 5 日
The following will give the lengths of the consecutive sequences of your vector:
q=yourvector;
a=diff(q);
b=find([a inf]>1);
c=diff([0 b]); length of the sequences
d=cumsum(c); endpoints of the sequences
  3 件のコメント
Laurent
Laurent 2013 年 9 月 5 日
編集済み: Laurent 2013 年 9 月 5 日
This is what I get:
>> c
c =
3 5 3 6 4
which are the lengths of the sequences.
>> d
d =
3 8 11 17 21
which is where the sequences end.
Then with a 'find(c>5)' you will know the location of the sequences larger than 5. Then from d you can deduce where the start of this sequence is.
Or did I misunderstand the question?
Image Analyst
Image Analyst 2013 年 9 月 5 日
No, sorry, I misunderstood your comment. I thought your endpoints was both endpoints - the starting and stopping indexes, but it's only where they stop.

サインインしてコメントする。


Jan
Jan 2013 年 9 月 8 日
編集済み: Jan 2013 年 9 月 8 日
This is almost a run-length problem:
x = [34 35 36 78 79 80 81 82 84 85 86 102 103 104 105 106 107 201 202 203 204];
[b, n, idx] = RunLength(x - (1:length(x)));
match = (n > 5);
result = x(idx(match));

David Sanchez
David Sanchez 2013 年 9 月 5 日
my_array = [34 35 36 78 79 80 81 82 84 85 86 102 103 104 105 106 107 201 202 203 204];
my_num = 0;
consec = 1;
for k= 1:(numel(my_array)-1)
if ( my_array(k+1) == (my_array(k) + 1) )
consec = consec +1;
if consec > 5
my_num = my_array(k-4);
break
end
else
consec = 1;
end
end
my_num =
201
  1 件のコメント
David Sanchez
David Sanchez 2013 年 9 月 5 日
I used this array instead:
my_array = [34 35 36 78 79 80 81 82 84 85 86 102 103 104 105 108 109 201 202 203 204 205 206 207 210];
my_num =
201
With the code above, the answer will be:
my_num =
102

サインインしてコメントする。


JEM
JEM 2017 年 5 月 30 日
編集済み: Walter Roberson 2021 年 6 月 5 日
Easier like this
t=[34 35 36 78 79 80 81 82 84 85 86 102 103 104 105 106 107 201 202 203 204];
% search for the derivative 1 1 1 1 1 corresponding to 5 consecutive values
result = t(findstr(diff(t),[1 1 1 1 1]));
  2 件のコメント
dpb
dpb 2018 年 10 月 16 日
The difference vector of ones will be N-1 length to be found, not N, though. Five differences==1 will correspond to six consecutive values incremented by one.
Sinem Balta Beylergil
Sinem Balta Beylergil 2021 年 6 月 5 日
One correction and it works perfectly:
t = find(overlapR==1);
result = t(intersect(diff(t),[1 1 1 1 1]))
PS [1 1 1 1 1] can be written as ones(1,5) and 5 can be any number of repetitions you want.

サインインしてコメントする。


Andrei Bobrov
Andrei Bobrov 2013 年 9 月 5 日
編集済み: Andrei Bobrov 2013 年 9 月 5 日
a = [34 35 36 78 79 80 81 82 84 85 86 102 103 104 105 106 107 201 202 203 204]
n = 6 % number consecutive numbers
k = [true;diff(a(:))~=1 ];
s = cumsum(k);
x = histc(s,1:s(end));
idx = find(k);
out = a(idx(x==n))
  2 件のコメント
Andrew Newell
Andrew Newell 2014 年 4 月 23 日
Doesn't work if a contains negative numbers.
Martti K
Martti K 2015 年 10 月 27 日
編集済み: Martti K 2015 年 10 月 27 日
To me it seems to work well with negative numbers also, but not with decreasing sequencies. In order to take the decreasing sequencies, use
k = [true;diff(a(:))~=-1];
In order to take at_least n consecutive numbers, use
out = a(idx(x>=n))

サインインしてコメントする。


Fahd Elabd
Fahd Elabd 2020 年 12 月 30 日
% Here I have get the first and last numbers of consecutive group in iOnes array,
iOnes = [34 35 36 78 79 80 81 82 84 85 86 102 103 104 105 106 107 201 202 203 204];
k=1;
j=1;
for i=1:length(iOnes)-1
if iOnes(i+1)-iOnes(i)==1 % means the next point is follwoing the current point
firstOnes(k) = iOnes(j);
lastOnes(k) = iOnes(i+1);
else
j=i+1;
k=k+1;
end
end

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by