annotate _FullBNT/KPMtools/normalize.m @ 9:4ea6619cb3f5 tip

removed log files
author matthiasm
date Fri, 11 Apr 2014 15:55:11 +0100
parents b5b38998ef3b
children
rev   line source
matthiasm@8 1 function [M, z] = normalise(A, dim)
matthiasm@8 2 % NORMALISE Make the entries of a (multidimensional) array sum to 1
matthiasm@8 3 % [M, c] = normalise(A)
matthiasm@8 4 % c is the normalizing constant
matthiasm@8 5 %
matthiasm@8 6 % [M, c] = normalise(A, dim)
matthiasm@8 7 % If dim is specified, we normalise the specified dimension only,
matthiasm@8 8 % otherwise we normalise the whole array.
matthiasm@8 9
matthiasm@8 10 if nargin < 2
matthiasm@8 11 z = sum(A(:));
matthiasm@8 12 % Set any zeros to one before dividing
matthiasm@8 13 % This is valid, since c=0 => all i. A(i)=0 => the answer should be 0/1=0
matthiasm@8 14 s = z + (z==0);
matthiasm@8 15 M = A / s;
matthiasm@8 16 elseif dim==1 % normalize each column
matthiasm@8 17 z = sum(A);
matthiasm@8 18 s = z + (z==0);
matthiasm@8 19 %M = A ./ (d'*ones(1,size(A,1)))';
matthiasm@8 20 M = A ./ repmatC(s, size(A,1), 1);
matthiasm@8 21 else
matthiasm@8 22 % Keith Battocchi - v. slow because of repmat
matthiasm@8 23 z=sum(A,dim);
matthiasm@8 24 s = z + (z==0);
matthiasm@8 25 L=size(A,dim);
matthiasm@8 26 d=length(size(A));
matthiasm@8 27 v=ones(d,1);
matthiasm@8 28 v(dim)=L;
matthiasm@8 29 %c=repmat(s,v);
matthiasm@8 30 c=repmat(s,v');
matthiasm@8 31 M=A./c;
matthiasm@8 32 end
matthiasm@8 33
matthiasm@8 34