How to subtract Mean from Matrix along columns

12 ビュー (過去 30 日間)
JFz
JFz 2017 年 11 月 16 日
編集済み: John D'Errico 2017 年 11 月 16 日
Hi,
In subtraction, there is an example of subtract mean from matrix: A - mean(A) and it works well. But I have a matrix A of 15X1000, and I want to subtract the mean mean(A, 2) along the 1000 columns, how to do that? Thanks Jennifer

採用された回答

JFz
JFz 2017 年 11 月 16 日
With the above help, found that bsxfun works for versions before 2017a:
bsxfun(@minus, A, mean(A, 2))

その他の回答 (2 件)

KL
KL 2017 年 11 月 16 日
編集済み: KL 2017 年 11 月 16 日
Just the same way, A-A_mean. Here's an example with 5 columns,
>>A = reshape(1:25,5,5) %dummydata
A =
1 6 11 16 21
2 7 12 17 22
3 8 13 18 23
4 9 14 19 24
5 10 15 20 25
>>A_mean = mean(A,2)
A_mean =
11
12
13
14
15
>>A = A-A_mean
A =
-10 -5 0 5 10
-10 -5 0 5 10
-10 -5 0 5 10
-10 -5 0 5 10
-10 -5 0 5 10
EDIT (thanks John)
and for older versions,
A = bsxfun(@minus,A,A_mean)
  3 件のコメント
JFz
JFz 2017 年 11 月 16 日
I tried the above but got this error: Error using - Matrix dimensions must agree.
I am using 2016a. I will have to change a lot of code to upgrade to 2017a successfully. Thanks, let me look at repmat and bsxfun.
JFz
JFz 2017 年 11 月 16 日
OK. bsxfun works! bsxfun(@minus, A, mean(A, 2))

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


JFz
JFz 2017 年 11 月 16 日
Thanks to KL and John! Have a nice day!

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by