using acummarray to average several columns at a time?

Technical Source
2 min readApr 13, 2022

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.

NOTE:-

Matlabsolutions.com provide latest MatLab Homework Help,MatLab Assignment Help for students, engineers and researchers in Multiple Branches like ECE, EEE, CSE, Mechanical, Civil with 100% output.Matlab Code for B.E, B.Tech,M.E,M.Tech, Ph.D. Scholars with 100% privacy guaranteed. Get MATLAB projects with source code for your learning and research.

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)

SEE COMPLETE ANSWER CLICK THE LINK

--

--

Technical Source

Simple! That is me, a simple person. I am passionate about knowledge and reading. That’s why I have decided to write and share a bit of my life and thoughts to.