]> Creatis software - CreaPhase.git/blob - octave_packages/miscellaneous-1.1.0/normc.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / miscellaneous-1.1.0 / normc.m
1 ## Copyright (C) 2011 Thomas Weber <tweber@debian.org>
2 ##
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
6 ## version.
7 ##
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
11 ## details.
12 ##
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/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {@var{x} =} normc (@var{M})
18 ## Normalize the columns of a matrix to a length of 1 and return the matrix.
19 ##
20 ## @example
21 ##   M=[1,2; 3,4];
22 ##   normc(M)
23 ##
24 ##   ans =
25 ##
26 ##   0.31623   0.44721
27 ##   0.94868   0.89443
28 ##
29 ## @end example
30 ## @seealso{normr}
31 ## @end deftypefn
32
33 function X = normc(M)
34   if (1 != nargin)
35     print_usage;
36   endif
37
38   X = normr(M.').';
39 endfunction
40
41 %% test for real and complex matrices
42 %!test
43 %! M = [1,2; 3,4];
44 %! expected = [0.316227766016838, 0.447213595499958;
45 %!             0.948683298050514, 0.894427190999916];
46 %! assert(normc(M), expected, eps);
47
48 %!test
49 %! M = [i,2*i; 3*I,4*I];
50 %! expected = [0.316227766016838*I, 0.447213595499958*I;
51 %!             0.948683298050514*I, 0.894427190999916*I];
52 %! assert(normc(M), expected, eps);
53
54 %!test
55 %! M = [1+2*I, 3+4*I; 5+6*I, 7+8*I];
56 %! expected = [0.123091490979333 + 0.246182981958665i, 0.255376959227625 + 0.340502612303499i;
57 %!             0.615457454896664 + 0.738548945875996i, 0.595879571531124 + 0.681005224606999i];
58 %! assert(normc(M), expected, 10*eps);
59
60 %% test error/usage handling
61 %!error <normc> normc();