X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=octave_packages%2Fmiscellaneous-1.1.0%2Fnormr.m;fp=octave_packages%2Fmiscellaneous-1.1.0%2Fnormr.m;h=7eee7a49836fbcc120749d070a634287c5aa1b17;hb=f5f7a74bd8a4900f0b797da6783be80e11a68d86;hp=0000000000000000000000000000000000000000;hpb=1705066eceaaea976f010f669ce8e972f3734b05;p=CreaPhase.git diff --git a/octave_packages/miscellaneous-1.1.0/normr.m b/octave_packages/miscellaneous-1.1.0/normr.m new file mode 100644 index 0000000..7eee7a4 --- /dev/null +++ b/octave_packages/miscellaneous-1.1.0/normr.m @@ -0,0 +1,60 @@ +## Copyright (C) 2011 Thomas Weber +## +## This program is free software; you can redistribute it and/or modify it under +## the terms of the GNU General Public License as published by the Free Software +## Foundation; either version 3 of the License, or (at your option) any later +## version. +## +## This program is distributed in the hope that it will be useful, but WITHOUT +## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +## details. +## +## You should have received a copy of the GNU General Public License along with +## this program; if not, see . + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{x} = } normr (@var{M}) +## Normalize the rows of a matrix to a length of 1 and return the matrix. +## +## @example +## M=[1,2; 3,4]; +## normr(M) +## +## ans = +## +## 0.44721 0.89443 +## 0.60000 0.80000 +## +## @end example +## @seealso{normc} +## @end deftypefn + +function X = normr(M) + if (1 != nargin) + print_usage; + endif + + norm = sqrt(sum(M .* conj(M),2)); + X = diag(1./norm) * M; +endfunction + +%% test for real and complex matrices +%!test +%! M = [1,2; 3,4]; +%! expected = [0.447213595499958, 0.894427190999916; 0.6, 0.8]; +%! assert(normr(M), expected, eps); + +%!test +%! M = [i,2*i; 3*I,4*I]; +%! expected = [0.447213595499958*I, 0.894427190999916*I; 0.6*I, 0.8*I]; +%! assert(normr(M), expected, eps); + +%!test +%! M = [1+2*I, 3+4*I; 5+6*I, 7+8*I]; +%! expected = [0.182574185835055 + 0.365148371670111i, 0.547722557505166 + 0.730296743340221i +%! 0.379049021789452 + 0.454858826147342i, 0.530668630505232 + 0.606478434863123i]; +%! assert(normr(M), expected, 10*eps); + +%% test error/usage handling +%!error normr();