function [t,Q]=ProcrustesFit(xi,x);
% PROCRUSTESFIT solves the Procrustes Problem 
%   [t,Q]=ProcristesFit(xi,x) computes an orthogonal matrix Q and a
%   shift t such that xi=Qx+t

xiq=sum(xi')/length(xi); xiq=xiq';
xq=sum(x')/length(x); xq=xq';
A=x-xq*ones(1,length(x));
B=xi-xiq*ones(1,length(xi));
[U,sigma,V]=svd(A*B');
Q=V*diag([ones(1,size(V,1)-1) det(V*U')])*U';
t=xiq-Q*xq;