Finding Number of Events with Values Consectively Greater Than a Threshold
Show older comments
I have temperature data called tmax (2650x1 double) that is basically just temperatures every single day in a row. Im trying to find the number of events in which the temperature was 90 degrees or above for 2 or more consecutive days. I've tried a few things with no luck, please help me with how to go about this. Thank you in advance for the help!
Accepted Answer
More Answers (2)
Walter Roberson
on 22 Jun 2020
Hint:
T = [0 0 0 0 1 0 0 0 1 1 0 0 0 0 1 1 1 0 0 0 0 1 1]
strfind(T, [0 1 1])
2 Comments
Claire Hollow
on 22 Jun 2020
Walter Roberson
on 22 Jun 2020
Using [0 idx is correct when you are looking for the beginning of patterns: it avoids problems if the part being searched begins with an appropriate pattern. Well done for discovering that.
Using idx 0] is not needed when searching for the beginning of a pattern: the extra 0 you are putting on can never match the 1 of your [0 1] pattern, and if the data to be searched happens to end in [0 1] then you would want that to be found when using [0 1] as the pattern. Adding the 0 does not do anything for you compared to using strfind properly.
Using idx 0] is, however, proper if you are looking for the end of a pattern.
ii=(ii2-ii1+1)>=2;
You do not need that. Consider
idx = tmax01(:).' > 90; %force row
N = 2; %number to find
ii1 = strfind([0 idx], [0 ones(1,N)]);
then ii1 will only match the beginning of locations that have a minimum of N 1's in a row. And for your purposes here, knowing the number of events, length(ii1) is all you need.
It is common to want to know the start and end of patterns, though. Instead of subtracting and comparing to a threshold, you can work more directly:
idx = tmax01(:).' > 90; %force row
N = 2; %number to find
ii1 = strfind([0 idx], [0, ones(1,N)]);
ii2 = strfind([idx 0], [ones(1,N), 0]) + N - 1;
Then you already know that ii1 and ii2 only match if the appropriate length was found, and ii1 and ii2 are direct indices to the beginning and end:
out = arrayfun(@(x,y) tmax0(x:y), ii1, ii2, 'uniform', 0)
Image Analyst
on 22 Jun 2020
Yet another way is to use regionprops() to measure all the stretches of days above 90:
% Generate 2650 random temperatures, and plot them in a bar chart.
numPoints = 2650;
dailyTemperatures = 80 + 12 * rand(numPoints,1);
bar(dailyTemperatures);
ylim([80, 100]);
grid on;
yline(90, 'Color', 'r', 'LineWidth', 2); % Display the threshold.
% Measure lengths of stretches above 90:
props = regionprops(dailyTemperatures > 90, 'Area')
allLengths = [props.Area]
% Count how events are 2 days or longer.
numEvents = sum(allLengths >= 2);
fprintf('There are %d heatwaves of more than 90 Degrees for 2 days or longer.\n', numEvents);

Doing it this way would also let you see the longest heat wave:
fprintf('The longest heatwave lasted %d days.\n', max(allLengths));
Categories
Find more on Read, Write, and Modify Image in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!