Calculate mean from daily to monthly values.

1 Ansicht (letzte 30 Tage)
lreplo
lreplo am 21 Jul. 2015
Kommentiert: lreplo am 22 Jul. 2015
Below is the code I have so far. It only spits out year 2014 month 12 mean. I need monthly means for all years. So there should be 84 values.
[y,m,d] = datevec(dn);
for j = [2008:2014]
for i= [1:12]
idx=find(y==j&m==i);
meandata1=mean(swh(idx));
meandata(84,:)=[84 meandata1];
end
end
Thank you in advance

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 21 Jul. 2015
Bearbeitet: Azzi Abdelmalek am 21 Jul. 2015
dn=datenum('01-01-2008','dd-mm-yyyy'):datenum('31-12-2014','dd-mm-yyyy')
swh=randi(10,numel(dn),1)
[y,m,d] = datevec(dn);
a=[y' m' d']
[ii,jj,kk]=unique(a(:,1:2),'rows')
out=accumarray(kk,(1:numel(kk))',[],@(x) mean(swh(x)))
  2 Kommentare
Azzi Abdelmalek
Azzi Abdelmalek am 21 Jul. 2015
If you want to correct your for loop
k=0;
for j = [2008:2014]
for i= [1:12]
k=k+1;
idx=find(y==j&m==i);
out1(k)=mean(swh(idx));
end
end
lreplo
lreplo am 22 Jul. 2015
Thank you! Worked perfectly.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Clocks and Timers finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by