MATLAB Answers

Average of different rows depending on the last column value

2 views (last 30 days)
Hariprasad
Hariprasad on 9 Mar 2016
Edited: Hariprasad on 9 Mar 2016
I have a 1000x5 matrix suppose wherein, first column is customer_id and last column is customer_desc which will be 1,2,3, or 4 for all the customers and columns 2 till 4 are data of which I need to take the mean depending on the value of customer_desc. Ex the output needs to be like this: -
Customer_desc Average col2 Average Col3 Average col4
----------------------------------------------------------------
1 0.235742555 0.325659159 0.283776929
2 0.511820228 0.311237444 0.394172541
3 0.254775313 0.352126843 0.306672852
4 0.365448276 0.345090639 0.354142646
I can do this using a for loop but I wanted to vectorize the calculation. Also I need to calculate the average of a customer_desc/ sum(average(other customer_desc)) as well. Any suggestions/ideas would be helpful.

  0 Comments

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 9 Mar 2016
If you have the Statistics and Machine Learning Toolbox, you can use grpstats().
means = grpstats(t, 'Customer_desc');

  2 Comments

Hariprasad
Hariprasad on 9 Mar 2016
I'm sorry, we do not have the Statistics and Machine Learning Toolbox. I have only Matlab and Database Toolbox.
Hariprasad
Hariprasad on 9 Mar 2016
Hi Image Analyst, I got the solution after searching.
% Group by customer_desc
[un,~,pos] = unique(data(:,end),'rows');
% Produce row, col subs
[col,row] = meshgrid(1:3,pos); % (1:3) seems to be num of columns
as below in reshape(data(:,2:4)[],1)
% Accumulate
Averages = [un accumarray([row(:), col(:)], reshape(data(:,2:4),[],1),[],@nanmean)];
Thanks for the help though. Need to work on the next step now.

Sign in to comment.

Sign in to answer this question.