How to normalize average column data in MATLAB?

I am trying to take a matrix and normalize the values ​​in each cell around the average for this column. To normalize, I mean subtract the value in each cell from the average value in this column, that is, subtract the average value for column 1 from the values ​​in column 1 ... subtract the average value for column N from the values ​​in column N. I'm looking for a script in Matlab . Thank!

+3
source share
4 answers

You can use the function meanto get the average value for each column, then the function bsxfunto subtract this from each column:

M = bsxfun(@minus, M, mean(M, 1));

, R2016b, , MATLAB . , :

M = M-mean(M, 1);
+10

mean . .

. , , repmat .

a=rand(10);
abar=mean(a);
abar=repmat(abar,size(a,1),1);
anorm=a-abar;

:

anorm=a-repmat(mean(a),size(a,1),1);
+3
% Assuming your matrix is in A
m = mean(A);
A_norm = A - repmat(m,size(A,1),1)
+1

As already mentioned, you will need a function meanthat, when called without additional arguments, gives the average value for each column in the input. Then a small complication arises, because you cannot just subtract the average value - its sizes differ from the original one.

So try the following:

a = magic(4)
b = a - repmat(mean(a),[size(a,1) 1]) % subtract columnwise mean from elements in a

repmat Replicates the average value according to the size of the data.

+1
source

Source: https://habr.com/ru/post/1757879/


All Articles