Reputation: 3980
I would like to find the index for when an array exceeds a certain value, and this value is value is exceeded for a duration, n. For examples:
n = 5;
dat = [1,2,2,1.5,2,4,2,1,1,3,4,6,8,4,9];
Here, I would like to find when 'dat' exceeds 2 for a duration greater than n for the first time. So, the solution here should lead to an answer:
ans = 10
Another example:
n = 7;
dat = [1,1,2,3,4,5,6,7,8,9,9,6,4,3,2,4,6,7,7,5];
find the first time that 'dat' exceeds or equals 5 for more than or equal to n times.
ans = 6
Upvotes: 0
Views: 2378
Reputation: 9075
n=5
x=2;
dat = [1,2,2,1.5,2,4,2,1,1,3,4,6,8,4,9];
vec= cumsum(dat>=x);
ind=find(vec>=n);
ind=dat(ind(1));
ind
will contain the answer 10
Upvotes: 1
Reputation: 45752
n = 5;
m = 2;
dat = [1,2,2,1.5,2,4,2,1,1,3,4,6,8,4,9];
c = conv(double(dat >= m), ones(1, n))
%I think you can also do
% c = conv((dat >= m)*1, ones(1, n))
min(find(c == n)) - n + 1
Upvotes: 2