Average of different rows depending on the last column value

조회 수: 1 (최근 30일)
Hariprasad
Hariprasad 2016년 3월 9일
편집: Hariprasad 2016년 3월 9일
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.

답변 (1개)

Image Analyst
Image Analyst 2016년 3월 9일
If you have the Statistics and Machine Learning Toolbox, you can use grpstats().
means = grpstats(t, 'Customer_desc');
  댓글 수: 2
Hariprasad
Hariprasad 2016년 3월 9일
편집: Hariprasad 2016년 3월 9일
I'm sorry, we do not have the Statistics and Machine Learning Toolbox. I have only Matlab and Database Toolbox.
Hariprasad
Hariprasad 2016년 3월 9일
편집: Hariprasad 2016년 3월 9일
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.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by