Question
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!
Expert Answer
Prashant Kumar
PhD Expert
Answered Aug 24, 2026
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:
Steps:
-
Threshold Comparison:
- Create a logical array that indicates whether each temperature is ≥90\geq 90.
-
Group Consecutive Days:
- Use
diffandfindto identify consecutive segments of ≥90\geq 90.
- Use
-
Count Events:
- Identify segments with 2 or more consecutive days and count them.
MATLAB Code:
% 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
100% Run Guarantee
3-Hour Fast-Track Delivery
Need a Custom Version or Complete Simulation for This Problem?
Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.
Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here
Related matrix Questions & Solutions
Browse All →
Explore similar technical troubleshooting questions and verified MATLAB solutions:
Ready-to-Run MATLAB & Simulink Projects
Browse All Projects →