Mercurial > hg > camir-aes2014
annotate toolboxes/FullBNT-1.0.7/KPMtools/normalize.m @ 0:e9a9cd732c1e tip
first hg version after svn
author | wolffd |
---|---|
date | Tue, 10 Feb 2015 15:05:51 +0000 |
parents | |
children |
rev | line source |
---|---|
wolffd@0 | 1 function [M, z] = normalise(A, dim) |
wolffd@0 | 2 % NORMALISE Make the entries of a (multidimensional) array sum to 1 |
wolffd@0 | 3 % [M, c] = normalise(A) |
wolffd@0 | 4 % c is the normalizing constant |
wolffd@0 | 5 % |
wolffd@0 | 6 % [M, c] = normalise(A, dim) |
wolffd@0 | 7 % If dim is specified, we normalise the specified dimension only, |
wolffd@0 | 8 % otherwise we normalise the whole array. |
wolffd@0 | 9 |
wolffd@0 | 10 if nargin < 2 |
wolffd@0 | 11 z = sum(A(:)); |
wolffd@0 | 12 % Set any zeros to one before dividing |
wolffd@0 | 13 % This is valid, since c=0 => all i. A(i)=0 => the answer should be 0/1=0 |
wolffd@0 | 14 s = z + (z==0); |
wolffd@0 | 15 M = A / s; |
wolffd@0 | 16 elseif dim==1 % normalize each column |
wolffd@0 | 17 z = sum(A); |
wolffd@0 | 18 s = z + (z==0); |
wolffd@0 | 19 %M = A ./ (d'*ones(1,size(A,1)))'; |
wolffd@0 | 20 M = A ./ repmatC(s, size(A,1), 1); |
wolffd@0 | 21 else |
wolffd@0 | 22 % Keith Battocchi - v. slow because of repmat |
wolffd@0 | 23 z=sum(A,dim); |
wolffd@0 | 24 s = z + (z==0); |
wolffd@0 | 25 L=size(A,dim); |
wolffd@0 | 26 d=length(size(A)); |
wolffd@0 | 27 v=ones(d,1); |
wolffd@0 | 28 v(dim)=L; |
wolffd@0 | 29 %c=repmat(s,v); |
wolffd@0 | 30 c=repmat(s,v'); |
wolffd@0 | 31 M=A./c; |
wolffd@0 | 32 end |
wolffd@0 | 33 |
wolffd@0 | 34 |