How to to calculate each decile average based on group?

2 views (last 30 days)
Hi guys,
I have a table with variables 'Date', 'Volumn'. Now I want to first group the variable by 'Date', then under each group, I want to calculate the average of first decile of 'Volumn'. Finally I want to generage a new table associates 'Date' and the calculated 'Average First Decile Volumn'. Is there any easy way to do this?
Thanks a lot.

Answers (1)

Jalaj Gambhir
Jalaj Gambhir on 30 Jul 2019
Edited: Jalaj Gambhir on 30 Jul 2019
Hi,
In a table, you can first group rows using unique date values in the Date column, then compute your required statistics using accumarray. You can refer to the example below:
dates = T.Date;
[uniqueDates,ua,uc] = unique(dates);
mean_T = accumarray(uc,T.Volumn,[],@mean);
new_table = table(uniqueDates,mean_T);
You can replace @mean with your function to compute average first decile. Refer prctile for the same.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!