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

조회 수: 5 (최근 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]));

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by