# Average of different rows depending on the last column value

조회 수: 1(최근 30일)
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.

로그인 to comment.

### 답변(1개)

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

#### 댓글 수: 2

I'm sorry, we do not have the Statistics and Machine Learning Toolbox. I have only Matlab and Database Toolbox.
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.

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.