MATLAB: Finding Patterns in Array

patternsranges

Hi, I am currently using the 'findpattern' function (which can be found here ) to find patterns in temperature data.
e.g. find the index when temperature was at 10 degrees for 2 hours. in this case my pattern would be [10 10 10 10 …] and findpattern.m would find the index in the start value. My temperature data was taken every 5 min which dictates the length of the pattern.
I would like to have a range of temperatures instead of 1 value i.e. find the locations when temperature was anywhere between 9 and 11 degrees for 2 hours.

Best Answer

%T: temperature vector
%desiredduration: number of samples where temperature must be between 9 and 11
inrange = T>=9 & T<= 11;
transitions = diff([false; inrange; false]); %find where the temperature change between in range/not in range occur
inrangestarts = find(transitions == 1);
inrangeends = find(transitions == -1);
inrangeduration = inrangeends - inrangestarts; %may be off by 1. Check
locs = inrangestarts(inrangeduration > desiredduration)