annotate _FullBNT/KPMtools/normalise.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 if ~(s == 1)
matthiasm@8 16 M = A / s;
matthiasm@8 17 else
matthiasm@8 18 M = A; % by matthiasm (saving memory)
matthiasm@8 19 end
matthiasm@8 20 elseif dim==1 % normalize each column
matthiasm@8 21 z = sum(A);
matthiasm@8 22 s = z + (z==0);
matthiasm@8 23 %M = A ./ (d'*ones(1,size(A,1)))';
matthiasm@8 24 M = A ./ repmatC(s, size(A,1), 1);
matthiasm@8 25 else
matthiasm@8 26 % Keith Battocchi - v. slow because of repmat
matthiasm@8 27 z=sum(A,dim);
matthiasm@8 28 s = z + (z==0);
matthiasm@8 29 L=size(A,dim);
matthiasm@8 30 d=length(size(A));
matthiasm@8 31 v=ones(d,1);
matthiasm@8 32 v(dim)=L;
matthiasm@8 33 %c=repmat(s,v);
matthiasm@8 34 c=repmat(s,v');
matthiasm@8 35 M=A./c;
matthiasm@8 36 end
matthiasm@8 37
matthiasm@8 38