1 ## Copyright (C) 2011 Thomas Weber <tweber@debian.org>
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
17 ## @deftypefn {Function File} {@var{x} = } normr (@var{M})
18 ## Normalize the rows of a matrix to a length of 1 and return the matrix.
38 norm = sqrt(sum(M .* conj(M),2));
39 X = diag(1./norm) * M;
42 %% test for real and complex matrices
45 %! expected = [0.447213595499958, 0.894427190999916; 0.6, 0.8];
46 %! assert(normr(M), expected, eps);
49 %! M = [i,2*i; 3*I,4*I];
50 %! expected = [0.447213595499958*I, 0.894427190999916*I; 0.6*I, 0.8*I];
51 %! assert(normr(M), expected, eps);
54 %! M = [1+2*I, 3+4*I; 5+6*I, 7+8*I];
55 %! expected = [0.182574185835055 + 0.365148371670111i, 0.547722557505166 + 0.730296743340221i
56 %! 0.379049021789452 + 0.454858826147342i, 0.530668630505232 + 0.606478434863123i];
57 %! assert(normr(M), expected, 10*eps);
59 %% test error/usage handling
60 %!error <normr> normr();