Count the frequency of 2 consecutive days in a year

2 vues (au cours des 30 derniers jours)
Eli
Eli le 24 Juin 2023
Dear all,
I want to obtain the frequency of 2 consecutive days in a year based on the following conditions/steps:
  1. I have a variable (R_3) with the daily data during 1992-2022.
  2. For a single year, obtain the days > 95th percentile. The 95th percentile is based on the entire period (1992-2022).
  3. Determine if there are any consecutive days. I have used backward difference for this.
  4. Determine if the consecutive days fall into the category of 2 consecutive days. If not ignore it.
  5. 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.

Réponses (1)

Parag Jhunjhunwala
Parag Jhunjhunwala le 24 Juin 2023
Modifié(e) : Parag Jhunjhunwala le 24 Juin 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

Catégories

En savoir plus sur Statistics and Machine Learning Toolbox dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by