MATLAB: Find where data is above threshold continuously and for how long using logical indexing

finding repeatsindexinglogical indexing

I believe this question must have been asked before, but I cannot find any answers. I have data using logical indexing have found times when the data is greater than 2 standard deviations of the mean. Now I would like to find the number of locations where the data is continuously above a threshold 25 times in a row, as well as the length of times when it is greater than 25 times in a row.
What I am attempting to do (in case logical indexing is not the best way) is find out how many times and how long my data is above 2 times the standard deviation of the mean for greater than 25 time points.

Best Answer

So you have some data vector, and a threshold. First thing is to generate a logical vector:
V = data > threshold;
then you can identify runs longer than 25. Here is a simpler example detecting runs with length three or more:
>> V = [0 1 0 0 1 1 1 0 0 0 1 1 1 1 0 1 1 0];
>> idx = find(diff([false,V,false]));
>> idb = idx(1:2:end);
>> ide = idx(2:2:end);
>> idy = (ide-idb)>=3; % run length >= 3
>> nnz(idy) % number of any such runs
ans =
2
>> ide(idy)-idb(idy) % lengths of those runs
ans =
3 4
Related Question