Finding weekly and yearly averages
Show older comments
I am trying to find weekly and yearly averages in my data, but I am running into trouble. I have hourly temperature data that begins in the middle of a week and a year but continues for multiple years. Each column of temperature data represents a different location that temperature data was taken. I was able to reshape the data to calculate daily and monthly averages, but I'm struggling with weekly and yearly averages. I've attached my data and some of the code. At the end you can see my sad attempt at reshaping the data into weeks. Thank you for your help!!
T = readtable('../Data/StrTempData/2016_CombinedSites/2016_07_11_StrTempSCC.xlsx');
[r1,c1]=size(T);
C = table2cell(T);
Ca = table2cell(T(3:r1,2:c1)); % temperature data
TOrgi = readMixedDates(C(:,1)); % date column with additional 2 rows on top
TOrg = TOrgi(3:length(TOrgi)-2); % date column
clear TOrgi;
sth = hour(TOrg(1));
dayi1 = (24-sth+1);
edh = hour(TOrg(length(TOrg)));
dayi2 =((length(TOrg)-edh)-1);
T1=TOrg(dayi1:dayi2)';
[r2,c2]= size(T1);
TM1= reshape(T1,24,[r2/24]);
dayv = TM1(1,:);
for i = 1:siteno;
TSitei= Ca(dayi1:dayi2,i);
TRSitei = reshape(TSitei,24,[r2/24]);
TRSiteiN = cell2mat(TRSitei);
TSiteiNaN= TRSiteiN;
TSiteiNaN(TSiteiNaN==-99)=NaN;
Aveday(:,i) = nanmean(TSiteiNaN)';
end
a = zeros(4,25);
fweek = [a; Aveday];
[r3,c3]= size(fweek);
weeksite = reshape(fweek,:,[r3/7])
Answers (1)
Andrei Bobrov
on 27 Jul 2016
Edited: Andrei Bobrov
on 27 Jul 2016
solution without datetime and table:
d = xlsread('2016_07_11_StrTempSCC.xlsx',1,'A4:Z16749');
T = d(:,2:end);
T(T == -99) = nan;
de = d(:,1) + datenum([1900 1 -1]);
% weekly
iwk = cumsum([1;diff(weekday(de) == 2) == 1]);
[iw,jw] = ndgrid(iwk,1:size(T,2));
z = accumarray(iwk,de,[],@(x){strjoin(cellstr(datestr(x([1,end]),...
'dd-mmm-yyyy')),' ... ')});
wkly = [z, num2cell(accumarray([iw(:),jw(:)],T(:),[],@nanmean))];
% yearly
[yr,~] = datevec(de);
[yar,~,iyr] = unique(yr);
[iy,jy] = ndgrid(iyr,1:size(T,2));
yrly = [yar, accumarray([iy(:),jy(:)],T(:),[],@nanmean)];
Categories
Find more on Dates and Time 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!