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

1 vue (au cours des 30 derniers jours)
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.

Réponse acceptée

Stephen23
Stephen23 le 22 Fév 2018
Modifié(e) : Stephen23 le 22 Fév 2018
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
  3 commentaires
Stephen23
Stephen23 le 22 Fév 2018
Modifié(e) : Stephen23 le 22 Fév 2018
@Systematically Neural: the false values concatenated onto each end of V are important if you want to write robust code and avoid pointless debugging later. They ensure that any sequence of ones right at the start or end of the vector will be detected properly. Without them (e.g. diff(V)) you will simply miss the starting index or end index. In the worst case, with a run of ones at the start and end, the detected indices will be completely inverted, i.e. you will actually be detecting runs of zeros, not of ones. Ouch!
Is it possible that V is a column vector? Here is a small change that will work for both row and column vectors, please give this a try:
idx = find(diff([false;V(:);false]));

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by