Mercurial > hg > camir-aes2014
comparison 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 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e9a9cd732c1e |
---|---|
1 function [M, z] = normalise(A, dim) | |
2 % NORMALISE Make the entries of a (multidimensional) array sum to 1 | |
3 % [M, c] = normalise(A) | |
4 % c is the normalizing constant | |
5 % | |
6 % [M, c] = normalise(A, dim) | |
7 % If dim is specified, we normalise the specified dimension only, | |
8 % otherwise we normalise the whole array. | |
9 | |
10 if nargin < 2 | |
11 z = sum(A(:)); | |
12 % Set any zeros to one before dividing | |
13 % This is valid, since c=0 => all i. A(i)=0 => the answer should be 0/1=0 | |
14 s = z + (z==0); | |
15 M = A / s; | |
16 elseif dim==1 % normalize each column | |
17 z = sum(A); | |
18 s = z + (z==0); | |
19 %M = A ./ (d'*ones(1,size(A,1)))'; | |
20 M = A ./ repmatC(s, size(A,1), 1); | |
21 else | |
22 % Keith Battocchi - v. slow because of repmat | |
23 z=sum(A,dim); | |
24 s = z + (z==0); | |
25 L=size(A,dim); | |
26 d=length(size(A)); | |
27 v=ones(d,1); | |
28 v(dim)=L; | |
29 %c=repmat(s,v); | |
30 c=repmat(s,v'); | |
31 M=A./c; | |
32 end | |
33 | |
34 |