필터 지우기
필터 지우기

how to get conservative

조회 수: 9 (최근 30일)
Norhadzni Abdul Halim
Norhadzni Abdul Halim 2019년 12월 12일
답변: BhaTTa 2024년 6월 14일
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.

답변 (1개)

BhaTTa
BhaTTa 2024년 6월 14일
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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by