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

2

5

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.

Minacious answered 12/5, 2013 at 14:34 Comment(2)
Eucleadian norm is the case.Minacious
then my answer should be useful for you.Mushy
D
4

Does your install of Matlab include the Neural Network Toolbox? If so, then try normr:

nA = normr(A);

Otherwise, @Shai's solution is good except that it won't handle infinite or NaN inputs – it's much safer to check undefined norm cases afterwards:

nA = bsxfun(@rdivide,A,sqrt(sum(A.^2,2)));
nA(~isfinite(nA)) = 1; % Use 0 to match output of @Shai's solution, Matlab's norm()

Note that normalizing of a zero length (all zero components) or infinite length vector (one or more components +Inf or -Inf) or one with a NaN component is not really well-defined. The solution above returns all ones, just as does Matlab's normr function. Matlab's norm function, however, exhibits different behavior. You may wish to specify a different behavior, e.g., a warning or an error, all zeros, NaNs, components scaled by the vector length, etc. This thread discusses the issue for zero-length vectors to some extent: How do you normalize a zero vector?.

Dolan answered 12/5, 2013 at 18:0 Comment(1)
Thank you, glad to find out about the easy way to do it!Minacious
M
6

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
Mushy answered 12/5, 2013 at 14:41 Comment(2)
Well, after using your code the norms for each row are ~0, instead of 1: 8.6903e-05; 1.3841e-04 1.6891e-04 2.0224e-04; 3.5168e-04; 4.0101e-04 5.7108e-04; 6.3513e-04; 6.6574e-04; 8.1582e-04; 8.5704e-04; 8.7563e-04; 0.0010; 0.0012; 0.0015; 0.0017;Minacious
@Minacious forgot to take sqrt of norm. See if my fix works now.Mushy
D
4

Does your install of Matlab include the Neural Network Toolbox? If so, then try normr:

nA = normr(A);

Otherwise, @Shai's solution is good except that it won't handle infinite or NaN inputs – it's much safer to check undefined norm cases afterwards:

nA = bsxfun(@rdivide,A,sqrt(sum(A.^2,2)));
nA(~isfinite(nA)) = 1; % Use 0 to match output of @Shai's solution, Matlab's norm()

Note that normalizing of a zero length (all zero components) or infinite length vector (one or more components +Inf or -Inf) or one with a NaN component is not really well-defined. The solution above returns all ones, just as does Matlab's normr function. Matlab's norm function, however, exhibits different behavior. You may wish to specify a different behavior, e.g., a warning or an error, all zeros, NaNs, components scaled by the vector length, etc. This thread discusses the issue for zero-length vectors to some extent: How do you normalize a zero vector?.

Dolan answered 12/5, 2013 at 18:0 Comment(1)
Thank you, glad to find out about the easy way to do it!Minacious

© 2022 - 2024 — McMap. All rights reserved.