using acummarray to average several columns at a time?

Illustration
Osnofa - 2022-04-13T12:07:50+00:00
Question: using acummarray to average several columns at a time?

Hello   I have a data array (mat) with the following dimensions: 149016x93 The columns are year | month | day | hour | data 1 | data 2 | data 3 | and so on until data 89 2001 | 1 | 1 | 0 | random numbers ... ... | ... | ... | ... | random numbers ... 2017 | 12 | 31 | 23 | random numbers ...   The data is random and it is what I want to average. I found this example (MathWorks example) and it is fine, however I've been strugling in how to run it over column 5 to 93...   [ah,~,ch] = unique(mat(:,2:4),'rows'); hraverage = [ah,accumarray(ch,mat(:,5),[],@nanmean)]; y problem is that I'm not being able to have as an output the 8784x93 array, only an 8784* x 4, I've tried loops but i'm missing something that I am not aware of...   *The dataset has several years of data. I want the hour average for each each day of the year. So it's 366 days * 24hours = 8784   for the sake of example, please feel free to consider a smaller array.   thank you for the attention! will keep digging on this...   sample data in attachment. randomly generated:   4 first collumns are: year, month, day, hour, and columns 5 to 7 are data columns.   the final result should be a 8784x7 file.

Expert Answer

Profile picture of Kshitij Singh Kshitij Singh answered . 2025-11-20

There's a simpler way of doing this with groupsummary
I imported the sampledata and made the table with 7 columns:
 
Year,Month,Day,Hour,VarName5,VarName6,VarName7
 
Then used the following commands to take advantage of binning in groupsummary and of being able to include empty groups:
 
 
sampledata.Time = datetime(sampledata.Year,sampledata.Month,sampledata.Day)
result = groupsummary(sampledata,{'Time','Hour'},{'dayofyear','none'},'mean',{'VarName5','VarName6','VarName7'},'IncludeEmptyGroups',1)

 


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!