Normalizing rows of matrix, so that their norm is equal to 1 (MATLAB)

Jacka picture Jacka · May 12, 2013 · Viewed 15.1k times · Source

I have a following problem - I have a matrix A of size 16x22440.

What I need to do is to normalize each row of this matrix, so that the norm of each of them is equal to 1 (for n=1:16 norm(A(n,:))==1)

How can I achieve that in matlab?

Edit: Each row in this matrix is a vector created of an 160x140 image and thus must be considered separately. The values need to be normalised to create an eigenfaces matrix.

Answer

Shai picture Shai · May 12, 2013

First, compute the norm (I assume Eucleadian norm here)

n = sqrt( sum( A.^2, 2 ) );
% patch to overcome rows with zero norm
n( n == 0 ) = 1;
nA = bsxfun( @rdivide, A, n ); % divide by norm