Slice matrix upon "group ID" to get the mean

조회 수: 3 (최근 30일)
Léon . 2012년 1월 2일
Hello,
I have a vector containing data, and a second one containing only group ids so I know which data point is member of which group. Now I want to compute the mean of each group and subtract that mean from each datapoint. That should be done group wise of course and that's the problem. I can only think of a very brutal loop solution that is awful slow (already tried it). I made a small example code to help you better understand my problem:
x = [1,2,3,4,5,6,7,8,9,10]; % Data
y = [1,1,1,2,2,1,2,1,1,5]; % Vector containing the group id
g1 = [1,2,3,6,8,9]; % Vector containing only data of group 1
g2 = [4,5,7]; % Vector containing only data of group 2
g3 = [10]; % Vector containing only data of group 3
So I don't know how to get g1:g3 or in other words, I don't know how to tell Matlab that it should create a vector m and store the mean of each group in that vector. Afterwards Matlab should subtract the mean from the data point.
The solution should look like this:
m = [(29/6),(16/3),10]; % Vector with the mean of each group
x_demeaned = [1-m(1),2-m(1),3-m(1),4-m(2),5-m(2),6-m(1), . . .]; % demeaned data
Can you help me here? Thanks in advance!
댓글 수: 1없음 표시없음 숨기기
Walter Roberson 2012년 1월 2일
There's probably a good use for accumarray here.

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

채택된 답변

Fangjun Jiang 2012년 1월 2일
x = [1,2,3,4,5,6,7,8,9,10]; % Data
y = [1,1,1,2,2,1,2,1,1,5]; % Vector containing the group id
[GroupId,indx_i,index_j]=unique(y);
GroupMean=arrayfun(@(k) mean(x(index_j==k)),1:length(GroupId))
New_x=x-GroupMean(index_j)
GroupMean =
4.8333 5.3333 10.0000
New_x =
Columns 1 through 7
-3.8333 -2.8333 -1.8333 -1.3333 -0.3333 1.1667 1.6667
Columns 8 through 10
3.1667 4.1667 0
댓글 수: 1없음 표시없음 숨기기
Léon 2012년 1월 3일
Thank you very much! I will learn that concept by heart. :-)

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

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by