% test Johnson-Lindestrauss lemma

% number of vectors
V=1000;
% dimension of fullspace
D = 1000;
% dimension of sub-space
S=100;
% create vectors
%vec=zeros(D,V);
%for k=1:V
% vec(:,k)=randn(D,1);
%end % for k
vec=randn(D,V);

D1=dist_mat(vec);
step=10;
finish=1000;
for k=step:step:finish
k
trun=vec([1:k],:);
D2=dist_mat(trun);
F1=D1(D1>0) ;
F2=D2(D2>0) ;
d(k)=max(F1./F2)/min(F1./F2);
end % for k

figure; grid on; hold on;
title(['Distortion of ',num2str(V),' points in ',num2str(D),'-space']);
plot([step:step:finish],d([step:step:finish]))
xlabel('dimension of projection');
ylabel('maximum distortion');

return

% find matrix of distances between column vectors in an array

function M=dist_mat(V)
[L,K]=size(V);
for k=1:K-1
v=V(:,k);
for j=k+1:K
w=V(:,j);
M(k,j)=norm(v-w) ;
end % for j
end % for k
end