Find consecutive values in a vector that are above a threshold
21 views (last 30 days)
I have a vector of monthly values. I would now like to identify periods of months where the values exceeded my threshold for 5 months. I know I can find all the individual months with the find function but how do I add this extra condition saying, only show me their position if five months in a row exceeded the threshold?
thank you so much for any hint. Sandra
Matt Kindig on 31 May 2013
Edited: Matt Kindig on 4 Jun 2013
s = RandStream('mcg16807','Seed',0);
RandStream.setDefaultStream(s); %set seed so example is reproducible
monthdata = rand(1,30); %random data
threshold = 0.4; %for example
aboveThreshold = (monthdata > threshold); %where above threshold
%aboveThreshold is a logical array, where 1 when above threshold, 0, below.
%we thus want to calculate the difference between rising and falling edges
aboveThreshold = [false, aboveThreshold, false]; %pad with 0's at ends
edges = diff(aboveThreshold);
rising = find(edges==1); %rising/falling edges
falling = find(edges==-1);
spanWidth = falling - rising; %width of span of 1's (above threshold)
wideEnough = spanWidth >= 5;
startPos = rising(wideEnough); %start of each span
endPos = falling(wideEnough)-1; %end of each span
%all points which are in the 5-month span (i.e. between startPos and endPos).
allInSpan = cell2mat(arrayfun(@(x,y) x:1:y, startPos, endPos, 'uni', false));
EDIT: Made more robust for case where first point is above threshold as well.