MATLAB: Normalization , colums, rows

columsmatrixnormalizationrows

Hi everyone,
May I know which way correct to do normalization for a matrix by colums or rows?
Thanks.

Best Answer

x = rand(242, 256),
xNRow = x ./ sum(x, 2); % Auto-expanding, Matlab >= R2016b
xNCol = x ./ sum(x, 1);
For older Matlab versions:
xNRow = bsxfun(@rdivide, x, sum(x, 2));
xNCol = bsxfun(@rdivide, x, sum(x, 1));
Now the rows or columns are normalized, such the the sum is 1.0. But perhaps you want the norm to be 1.0?
xNRow = x ./ vecnorm(x, 2); % Auto-expanding, vecnorm needs >= R2017b
xNCol = x ./ vecnorm(x, 1);
Or with older Matlab versions:
xNRow = x ./ sqrt(sum(x .* x), 2)); % Auto-expanding, >= R2016b
xNCol = x ./ sqrt(sum(x .* x), 1));
or again with bsxfun.
There are more methods for a "normalization": Set the mean to zero, and/or the std to 1 or such that the maximum peak height is 1.0. So you have to find out, what you need mathematically. Then the implementation in Matlab is easy.