Finding Number of Events with Values Consectively Greater Than a Threshold

Illustration
Mahikaserawat - 2020-07-29T13:51:34+00:00
Question: Finding Number of Events with Values Consectively Greater Than a Threshold

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

Profile picture of Prashant Kumar 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:


Steps:

  1. Threshold Comparison:

    • Create a logical array that indicates whether each temperature is ≥90\geq 90.
  2. Group Consecutive Days:

    • Use diff and find to identify consecutive segments of ≥90\geq 90.
  3. 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);
 

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:

  1. Threshold Comparison:

    • Create a logical array that indicates whether each temperature is ≥90\geq 90.
  2. Group Consecutive Days:

    • Use diff and find to identify consecutive segments of ≥90\geq 90.
  3. Count Events:

    • Identify segments with 2 or more consecutive days and count them.

MATLAB Code:

matlab
% 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);

Explanation of Key Functions:

  1. tmax >= 90:

    • Creates a logical array where 1 indicates temperatures ≥90\geq 90.
  2. bwlabel:

    • Labels connected components (consecutive 1s) in the logical array.
  3. unique and sum:

    • Used to compute the size of each labeled group and check if it meets the 2-day threshold.

Example Output:

For the tmax array in the example:

 

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

Let me know if you'd like further clarification or enhancements!


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!