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

11 ビュー (過去 30 日間)
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.

採用された回答

Stephen23
Stephen23 2018 年 2 月 22 日
編集済み: Stephen23 2018 年 2 月 22 日
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 件のコメント
Stephen23
Stephen23 2018 年 2 月 22 日
編集済み: Stephen23 2018 年 2 月 22 日
@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]));
Systematically Neural
Systematically Neural 2018 年 2 月 22 日
Yes, that fixed it. Thanks!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by