mdsfast.m

来自「流形学习程序」· M 代码 · 共 49 行

M
49
字号
function [points]=mdsFast(d,dim)
% --- function mdsFast for Multi-Dimensional Scaling
% Written by Michael D. Lee.
% Lee recommends metric=2, iterations=50, learnrate=0.05.

[n, check] = size(d);
iterations = 30;
lr=0.05;   % learnrate
r=2;   % metric
% normalise distances to lie between 0 and 1
reshift=min(min(d));
d=d-reshift;
rescale=max(max(d));
d=d/rescale;
% calculate the variance of  the distance matrix
dbar=(sum(sum(d))-trace(d))/n/(n-1);
temp=(d-dbar*ones(n)).^2;
vard=.5*(sum(sum(temp))-trace(temp));
% initialize variables
its=0;
p=rand(n,dim)*.01-.005;
dh=zeros(n);
rinv=1/r;  %PT
kk=1:dim;  %PT 
% main loop for the given number of iterations
while (its<iterations)
   its=its+1;
   % randomly permute the objects to determine the order
   % in which they are pinned for this iteration
   pinning_order=randperm(n);
   for i=1:n
      m=pinning_order(i);
      % having pinned an object, move all of the other on each dimension
      % according to the learning rule   
      
      %PT: Vectorized the procedure, gives factor 6 speed up for n=100 dim=2
      indx=[1:m-1 m+1:n];                                                       
      pmat=repmat(p(m,:),[n 1])-p;                                              
      dhdum=sum(abs(pmat).^r,2).^rinv;
      dh(m,indx)=dhdum(indx)';
      dh(indx,m)=dhdum(indx);
      dhmat=lr*repmat((dhdum(indx)-d(m,indx)').*(dhdum(indx).^(1-r)),[1 dim]);
      p(indx,kk)=p(indx,kk)+dhmat.*abs(pmat(indx,kk)).^(r-1).*sign(pmat(indx,kk));
                    %plus sign in learning rule is due the sign of pmat
   end;
end;
points = p*rescale+reshift;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?