Clear Filters
Clear Filters

how to get conservative

9 views (last 30 days)
Norhadzni Abdul Halim
Norhadzni Abdul Halim on 12 Dec 2019
Answered: BhaTTa on 14 Jun 2024
Hello..can anyone help me how to determine for each month the number of two or three or five consecutive days that rain based on the rainfall data.
And the solving must used Loop.
Thanks.

Answers (1)

BhaTTa
BhaTTa on 14 Jun 2024
Given that your rainfall data is organized in a 31x12 array, where each row represents a day of the month (up to 31 days) and each column represents a month of the year, the approach to counting consecutive rainy days will be slightly adjusted to accommodate this structure. We'll still use loops to go through each month and then each day, keeping track of consecutive rainy days and resetting the count appropriately.
Here's how you can do it:
Step 1: Define the Rainy Day Threshold
First, decide what minimum amount of rainfall (in mm) qualifies as a "rainy day." For this example, any day with rainfall greater than 0 mm will be considered rainy.
Step 2: Initialize Variables
Initialize variables to store counts of sequences of 2, 3, and 5 consecutive rainy days for each month.
Step 3: Loop Through the Array
You'll loop through each column (month) of the array, then loop through each row (day) within that column to check for consecutive rainy days.
% Assume rainfallData is your 31x12 array
rainfallData = randi([0, 20], 31, 12); % Example data
% Rainy day threshold
threshold = 0;
% Initialize counts (for demonstration, we'll store these counts in a 12x3 matrix,
% where each row represents a month and each column represents counts for 2, 3, and 5 consecutive days)
monthlyCounts = zeros(12, 3);
for month = 1:12
count = 0; % Counter for consecutive rainy days
for day = 1:31
if day <= size(rainfallData, 1) && rainfallData(day, month) > threshold
count = count + 1;
else
% Check if the sequence just ended qualifies for 2, 3, or 5 days
if count == 2
monthlyCounts(month, 1) = monthlyCounts(month, 1) + 1;
elseif count == 3
monthlyCounts(month, 2) = monthlyCounts(month, 2) + 1;
elseif count == 5
monthlyCounts(month, 3) = monthlyCounts(month, 3) + 1;
end
count = 0; % Reset count for the next sequence
end
end
% Check for sequences that might end at the last day of the month
if count == 2
monthlyCounts(month, 1) = monthlyCounts(month, 1) + 1;
elseif count == 3
monthlyCounts(month, 2) = monthlyCounts(month, 2) + 1;
elseif count == 5
monthlyCounts(month, 3) = monthlyCounts(month, 3) + 1;
end
end
% Display the results
for month = 1:12
fprintf('Month %d: 2-day sequences: %d, 3-day sequences: %d, 5-day sequences: %d\n', ...
month, monthlyCounts(month, 1), monthlyCounts(month, 2), monthlyCounts(month, 3));
end

Categories

Find more on Loops and Conditional Statements 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!