using acummarray to average several columns at a time?

Osnofa · Apr 13, 2022 · 2.3K views
Question
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 PhD Expert
Answered Aug 25, 2026
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)

 

100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!