필터 지우기
필터 지우기

How to get the average of a subset of data in a matrix?

조회 수: 5 (최근 30일)
Aaron
Aaron 2013년 11월 5일
댓글: Aaron 2013년 11월 6일
I am having some difficulty finding the average of some subsets of data. For example, given the following matrix:
ItemA 7
ItemA 8
ItemA 9
ItemB 10
ItemB 12
ItemB 14
ItemC 8
ItemC 16
ItemC 0
Given the above data, I would like to find the average of item A, B, and C individually. So it should return that Item A the average is 8, Item B the average is 12, and item C the average is 8.
The above data is just an example, the actual data is many more items so I need it to work for an unknown amount of items.
If you have any questions feel free to ask,
Thanks
  댓글 수: 7
Aaron
Aaron 2013년 11월 5일
If the mean of the x's and the mean of the y's are the same as kmeans of one cluster then I would be able to just use accummarray right?
Aaron
Aaron 2013년 11월 5일
Also, I think kmeans tries to minimize the Euclidean distances to give a centroid with the minimal distance between all of the data points. I am open for suggestions though

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

채택된 답변

Andrei Bobrov
Andrei Bobrov 2013년 11월 5일
C = {'ItemA' 1 3
'ItemA' 2 5
'ItemA' 3 2
'ItemB' 2 4
'ItemB' 1 3
'ItemC' 2 9
'ItemC' 9 2};
[a,ii,ii] = unique(C(:,1));
[j1,j2] = ndgrid(ii,1:2);
b = cat(1,C{:,2:3});
out = [a, accumarray([j1(:),j2(:)],b,[],@(x){mean(x)})];
  댓글 수: 1
Aaron
Aaron 2013년 11월 6일
That worked out perfectly. Is there a way to do standard deviation in the accumarray function instead of mean? like @stddev or something similar. Thanks a lot for your answer, it was very useful.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 ANOVA에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by