annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/pcaproj.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 [P,V,me,l] = pcaproj(D,arg1,arg2)
Daniel@0 2
Daniel@0 3 %PCAPROJ Projects data vectors using Principal Component Analysis.
Daniel@0 4 %
Daniel@0 5 % [P,V,me,l] = pcaproj(D, odim)
Daniel@0 6 % P = pcaproj(D, V, me)
Daniel@0 7 %
Daniel@0 8 % Input and output arguments ([]'s are optional)
Daniel@0 9 % D (matrix) size dlen x dim, the data matrix
Daniel@0 10 % (struct) data or map struct
Daniel@0 11 % odim (scalar) how many principal vectors are used
Daniel@0 12 %
Daniel@0 13 % P (matrix) size dlen x odim, the projections
Daniel@0 14 % V (matrix) size dim x odim, principal eigenvectors (unit length)
Daniel@0 15 % me (vector) size 1 x dim, center point of D
Daniel@0 16 % l (vector) size 1 x odim, the corresponding eigenvalues,
Daniel@0 17 % relative to total sum of eigenvalues
Daniel@0 18 %
Daniel@0 19 % See also SAMMON, CCA.
Daniel@0 20
Daniel@0 21 % Contributed to SOM Toolbox 2.0, February 2nd, 2000 by Juha Vesanto
Daniel@0 22 % Copyright (c) by Juha Vesanto
Daniel@0 23 % http://www.cis.hut.fi/projects/somtoolbox/
Daniel@0 24
Daniel@0 25 % juuso 191297 070200
Daniel@0 26
Daniel@0 27 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 28
Daniel@0 29 error(nargchk(2, 3, nargin)); % check the number of input arguments
Daniel@0 30
Daniel@0 31 % the data
Daniel@0 32 if isstruct(D),
Daniel@0 33 if strcmp(D.type,'som_map'), D=D.codebook; else D=D.data; end
Daniel@0 34 end
Daniel@0 35 [dlen dim] = size(D);
Daniel@0 36
Daniel@0 37 if nargin==2,
Daniel@0 38
Daniel@0 39 odim = arg1;
Daniel@0 40
Daniel@0 41 % autocorrelation matrix
Daniel@0 42 A = zeros(dim);
Daniel@0 43 me = zeros(1,dim);
Daniel@0 44 for i=1:dim,
Daniel@0 45 me(i) = mean(D(isfinite(D(:,i)),i));
Daniel@0 46 D(:,i) = D(:,i) - me(i);
Daniel@0 47 end
Daniel@0 48 for i=1:dim,
Daniel@0 49 for j=i:dim,
Daniel@0 50 c = D(:,i).*D(:,j); c = c(isfinite(c));
Daniel@0 51 A(i,j) = sum(c)/length(c); A(j,i) = A(i,j);
Daniel@0 52 end
Daniel@0 53 end
Daniel@0 54
Daniel@0 55 % eigenvectors, sort them according to eigenvalues, and normalize
Daniel@0 56 [V,S] = eig(A);
Daniel@0 57 eigval = diag(S);
Daniel@0 58 [y,ind] = sort(abs(eigval));
Daniel@0 59 eigval = eigval(flipud(ind));
Daniel@0 60 V = V(:,flipud(ind));
Daniel@0 61 for i=1:odim, V(:,i) = (V(:,i) / norm(V(:,i))); end
Daniel@0 62
Daniel@0 63 % take only odim first eigenvectors
Daniel@0 64 V = V(:,1:odim);
Daniel@0 65 l = abs(eigval)/sum(abs(eigval));
Daniel@0 66 l = l(1:odim);
Daniel@0 67
Daniel@0 68 else % nargin==3,
Daniel@0 69
Daniel@0 70 V = arg1;
Daniel@0 71 me = arg2;
Daniel@0 72 odim = size(V,2);
Daniel@0 73 D = D-me(ones(dlen,1),:);
Daniel@0 74
Daniel@0 75 end
Daniel@0 76
Daniel@0 77 % project the data using odim first eigenvectors
Daniel@0 78 P = D*V;
Daniel@0 79
Daniel@0 80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%