简体   繁体   中英

count number values exceeds given threshold in moving window in matlab

I have time vs values plot. time =100. I want to select time 1 to 4 & then count how many values are exceeding 20. ie for time 1 to 4 values are 16 43 94 21 so 3 values are exceeding 20 so count should be 3. then want move window so time is 2 to 5 & count number of values exceeding 20. so last window would be 97 to 100. I tried following code but it showing 0 & 1

N=4;% length of window
d=length(t);% t has 100 values so took length
for e=0:d-N;
    for x=1+e:N+e;
        y(x)=sum(t(x)>20); % t contains values so took t(x)    
    end
end

how to do it.

You can use a logical index showing where t is greater than 20 then use movsum to count how many values in sliding window exceed 20 ;

N =4;
idx = t > 20;
result = movsum(idx,N)

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