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!
Prashant Kumar answered .
2025-11-20
To count the number of events in which the temperature stays at or above 90 degrees for 2 or more consecutive days, you can use MATLAB's logical indexing and a few key functions. Here's how you can do it:
Threshold Comparison:
Group Consecutive Days:
diff and find to identify consecutive segments of ≥90\geq 90.Count Events:
% Example temperature data (replace with your tmax data)
tmax = [85; 91; 93; 89; 90; 92; 88; 90; 90; 85; 91; 91; 92];
% Step 1: Create a logical array for temperatures >= 90
aboveThreshold = tmax >= 90;
% Step 2: Identify groups of consecutive days
% Use diff to find transitions and bwlabel to label groups
groupLabels = bwlabel(aboveThreshold);
% Step 3: Count group lengths and filter for events >= 2 days
uniqueGroups = unique(groupLabels);
numEvents = 0;
for i = 1:length(uniqueGroups)
if uniqueGroups(i) == 0
continue; % Skip the '0' group, which represents no event
end
groupSize = sum(groupLabels == uniqueGroups(i));
if groupSize >= 2
numEvents = numEvents + 1;
end
end
% Display the result
fprintf('Number of events with 2 or more consecutive days >= 90: %d\n', numEvents);
tmax = [85; 91; 93; 89; 90; 92; 88; 90; 90; 85; 91; 91; 92];
The output will be: javascript Copy Edit
Number of events with 2 or more consecutive days >= 90: 3