フィルターのクリア

Slice matrix upon "group ID" to get the mean

3 ビュー (過去 30 日間)
Léon
Léon 2012 年 1 月 2 日
回答済み: Lola Davidson 2024 年 6 月 4 日
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
Walter Roberson 2012 年 1 月 2 日
There's probably a good use for accumarray here.

サインインしてコメントする。

採用された回答

Fangjun Jiang
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
Léon 2012 年 1 月 3 日
Thank you very much! I will learn that concept by heart. :-)

サインインしてコメントする。

その他の回答 (1 件)

Lola Davidson
Lola Davidson 2024 年 6 月 4 日
For those stumbling on this more recently, MATLAB now has the grouptransform function introduced in R2018b. It can be used to make grouped calculations where the result is the same size as the input. It even has a built-in method for subtracting off group means:
x = [1,2,3,4,5,6,7,8,9,10]';
y = [1,1,1,2,2,1,2,1,1,5]';
grouptransform(x,y,"meancenter")
ans = 10x1
-3.8333 -2.8333 -1.8333 -1.3333 -0.3333 1.1667 1.6667 3.1667 4.1667 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by