annotate toolboxes/FullBNT-1.0.7/KPMtools/normalise.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function [M, z] = normalise(A, dim)
Daniel@0 2 % NORMALISE Make the entries of a (multidimensional) array sum to 1
Daniel@0 3 % [M, c] = normalise(A)
Daniel@0 4 % c is the normalizing constant
Daniel@0 5 %
Daniel@0 6 % [M, c] = normalise(A, dim)
Daniel@0 7 % If dim is specified, we normalise the specified dimension only,
Daniel@0 8 % otherwise we normalise the whole array.
Daniel@0 9
Daniel@0 10 if nargin < 2
Daniel@0 11 z = sum(A(:));
Daniel@0 12 % Set any zeros to one before dividing
Daniel@0 13 % This is valid, since c=0 => all i. A(i)=0 => the answer should be 0/1=0
Daniel@0 14 s = z + (z==0);
Daniel@0 15 M = A / s;
Daniel@0 16 elseif dim==1 % normalize each column
Daniel@0 17 z = sum(A);
Daniel@0 18 s = z + (z==0);
Daniel@0 19 %M = A ./ (d'*ones(1,size(A,1)))';
Daniel@0 20 M = A ./ repmatC(s, size(A,1), 1);
Daniel@0 21 else
Daniel@0 22 % Keith Battocchi - v. slow because of repmat
Daniel@0 23 z=sum(A,dim);
Daniel@0 24 s = z + (z==0);
Daniel@0 25 L=size(A,dim);
Daniel@0 26 d=length(size(A));
Daniel@0 27 v=ones(d,1);
Daniel@0 28 v(dim)=L;
Daniel@0 29 %c=repmat(s,v);
Daniel@0 30 c=repmat(s,v');
Daniel@0 31 M=A./c;
Daniel@0 32 end
Daniel@0 33
Daniel@0 34