简体   繁体   中英

matlab - find index for when values exceeds a threshold n number of times

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

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
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM