Have a looping sample with index 1-5 through time, I need averages for each period and index (not simple group-wise mean of id 1-5). Complicated by occasional non-even sample numbers, i.e. usually there are 2 data points for each id, but occasionally 3 and also by the hh and mm time stamp not changing in step with id, i.e. the same minute could exist for several id's. Data like so (actual dataset larger and more complicated):
yyyy mm dd hh mm rec id data1 data22015 5 16 12 54 65433 1 402.2262 3.5981172015 5 16 12 54 65434 1 401.9153 3.5965882015 5 16 12 54 65435 2 401.9349 3.5970932015 5 16 12 54 65436 2 402.1344 3.5984882015 5 16 12 54 65437 3 401.6603 3.5992192015 5 16 12 55 65438 3 402.3899 3.5968392015 5 16 13 3 65439 4 401.6629 3.5749182015 5 16 13 3 65440 4 401.9409 3.5753832015 5 16 13 3 65441 5 402.3803 3.5746252015 5 16 13 3 65442 5 402.174 3.5717422015 5 16 13 24 65443 1 401.9677 3.5688592015 5 16 3 24 65444 1 401.7614 3.565976
Could use a moving window-type approach:
for i = 1:floor(n/(period) means1(i) = mean(T1((i*period-(period-1)):(i*period))); means2(i) = mean(T2((i*period-(period-1)):(i*period)));...but occasionally there are less than expected numbers in the period.
This is nice solution if I wanted all ID 1 etc means:
[ii,jj] = ndgrid(id,1:size(data,2));bb = accumarray([ii(:),jj(:)], data(:), [], @mean);meanout = data - bb(id,:);
So what I want as result is:
id n mean1 mean21 2 402.07075 3.59735252 2 402.03465 3.59779053 2 402.0251 3.5980294 2 401.8019 3.57515055 2 402.27715 3.57318351 2 401.86455 3.5674175
Any advice appreciated, could not find info for this specific issue. Some clever use of accumarray will probably work, but I could not conquer it.
Best Answer