Count the frequency of 2 consecutive days in a year
4 views (last 30 days)
Show older comments
Dear all,
I want to obtain the frequency of 2 consecutive days in a year based on the following conditions/steps:
- I have a variable (R_3) with the daily data during 1992-2022.
- For a single year, obtain the days > 95th percentile. The 95th percentile is based on the entire period (1992-2022).
- Determine if there are any consecutive days. I have used backward difference for this.
- Determine if the consecutive days fall into the category of 2 consecutive days. If not ignore it.
- Total count in (4). E.g., 2 consecutive days fall on the 1st, 2nd, & 5th, 6th. This would give me 2 counts of 2 consecutive days.
I have attached my code below. I am having trouble with steps 4 & 5 and do not know how to proceed.
clear; clc;
load('Sample.mat');
% % Estimate the number of days during 1992-2022 ==========================
yr1 = 1992;
yr2 = 2022;
yr = yr1:yr2;
d1 = yeardays(yr); % Number of days per year
d2 = [1 d1(1:end-1)];
t1 = cumsum(d2);
t2 = cumsum(d1);
y1 = R_3;
for i = 1:size(t1,2)
a1{i,:} = find(y1(t1(i):t2(i)) > prctile(y1,95)); % Days where y1 > 95th percentile
a2 = cell2mat(a1(i));
a3 = diff(a2); % Backward difference
a4{i,:} = find(a3 == 1); % 1 == consecutive day
end
Thank you very much for your help.
0 Comments
Answers (1)
Parag Jhunjhunwala
on 24 Jun 2023
Edited: Parag Jhunjhunwala
on 24 Jun 2023
Hi Eli
I have modified the for loop in your code to find out the count of exactly 2 consecutive days for each year and stored it in the vector a3.
for i = 1:size(t1,2)
a1{i,:} = find(y1(t1(i):t2(i)) > prctile(y1,95)); % Days where y1 > 95th percentile
a2 = cell2mat(a1(i));
count=0;
a3(i) = 0; % Count exactly 2 consecutive days
for j=2:length(a2)
if(a2(j)-a2(j-1) == 1)
count = count+1;
else
if(count == 1)
a3(i) = a3(i)+1;
end
count=0;
end
end
if(count == 1)
a3(i) = a3(i)+1;
end
end
0 Comments
See Also
Categories
Find more on Logical 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!