How can I calculate the mean of certain rows of a matrix based on specific column values?

1 回表示 (過去 30 日間)
I have a matrix with 2 columns: example: A=[ 123 1; 444 2; 634 1; 311 1; 111 2; . . . 222 1; 312 1;]
First.I want to find the mean of the values in column 1 based on the values of column 2. Means for value 1(mean1) and for value 2(mean2).
Second. Ι want to subtract the means from the respective rows.The mean1 from the rows in column 1 that correspond to value 1 in column 2 and the mean2 from the rows in column 1 that correspond to 2 value in column 2. And take a column of the residuals without spoiling the sequence of the values.
thanks!

採用された回答

Star Strider
Star Strider 2017 年 1 月 7 日
編集済み: Star Strider 2017 年 1 月 7 日
This works:
A = [ 123 1; 444 2; 634 1; 311 1; 111 2; 222 1; 312 1];
Amean = accumarray(A(:,2), A(:,1), [], @mean) % Calculate Means
AmeanMtx = [Amean [1;2]] % Amean For The Appropriate Column #2 (For Demonstration Only, Can Be Deleted)
Aresd = A(:,1)-Amean(A(:,2)) % Use The Second Column To Index ‘Amean’ To Calculate The Residuals
Amean =
320.4
277.5
AmeanMtx =
320.4 1
277.5 2
Aresd =
-197.4
166.5
313.6
-9.4
-166.5
-98.4
-8.4
EDIT Added comment documentation.
  2 件のコメント
Panos Ale
Panos Ale 2017 年 1 月 7 日
Thank you very much! It's perfect!!!
Star Strider
Star Strider 2017 年 1 月 7 日
As always, my pleasure! Thank you!

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by