Hi everyone,
I have 19 years of data and I have to compute lognormal distribution of each month of each year. I've already computed monthly average values and monthly standard deviation on each year. I know the more quickly way to do this kind of operation is for loop but I'm a beginner on Matlab and i don't know how to use for loop i these situations. I think that I need of two for loop, one with index on the years and the other with index on the months but I don't know. I've done the code below that it is manual and too much long. I need to make it authomatic. Thank you in advance!
format long gfolder = 'D:\Valerio\data\IPCC_midcent\RCP4.5\BCC_CSM\BCC_CSM.xlsx';file = xlsread(folder);dt = datetime([file(:,1:3) file(:,4)/1E4 repmat([0 0],size(file,1),1)]);tt = timetable(dt, file(:,5:end));data = tt.Var1;% file(:, 3:4) = []; % delete day and hour columns as they are not important for yearly mean
[grps, Years, Months] = findgroups(file(:,1), file(:,2));result_mean = splitapply(@(x) mean(x, 1), file(:,5:end), grps);result_mean = [Years Months result_mean];result_std = splitapply(@(x) std(x, [], 1), file(:,5:end), grps);result_std = [Years Months result_std];TR1 = timerange('01-Jan-2026 00:00:00','01-Feb-2026 00:00:00');tt_TR1 = tt(TR1,:);Hs1 = tt_TR1.Var1(:,1);Tp1 = tt_TR1.Var1(:,2);d_Hs1 = lognpdf(Hs1,result_mean(1,3),result_std(1,3));d_Tp1 = lognpdf(Tp1,result_mean(1,4),result_std(1,4));TR2 = timerange('01-Feb-2026 00:00:00','01-Mar-2026 00:00:00');tt_TR2 = tt(TR2,:);Hs2 = tt_TR2.Var1(:,1);Tp2 = tt_TR2.Var1(:,2);d_Hs2 = lognpdf(Hs2,result_mean(2,3),result_std(2,3));d_Tp2 = lognpdf(Tp2,result_mean(2,4),result_std(2,4));TR3 = timerange('01-Mar-2026 00:00:00','01-Apr-2026 00:00:00');tt_TR3 = tt(TR3,:);Hs3 = tt_TR3.Var1(:,1);Tp3 = tt_TR3.Var1(:,2);d_Hs3 = lognpdf(Hs3,result_mean(3,3),result_std(3,3));d_Tp3 = lognpdf(Tp3,result_mean(3,4),result_std(3,4));TR4 = timerange('01-Apr-2026 00:00:00','01-May-2026 00:00:00');tt_TR4 = tt(TR4,:);Hs4 = tt_TR4.Var1(:,1);Tp4 = tt_TR4.Var1(:,2);d_Hs4 = lognpdf(Hs4,result_mean(4,3),result_std(4,3));d_Tp4 = lognpdf(Tp4,result_mean(4,4),result_std(4,4));TR5 = timerange('01-May-2026 00:00:00','01-Jun-2026 00:00:00');tt_TR5 = tt(TR5,:);Hs5 = tt_TR5.Var1(:,1);Tp5 = tt_TR5.Var1(:,2);d_Hs5 = lognpdf(Hs5,result_mean(5,3),result_std(5,3));d_Tp5 = lognpdf(Tp5,result_mean(5,4),result_std(5,4));
Best Answer