title: Correct implementation of The Johnson-Lindenstrauss lemma
body: <p>I am trying to implement the <a href="http://tcs.nju.edu.cn/wiki/index.php/%E9%9A%8F%E6%9C%BA%E7%AE%97%E6%B3%95_%28Fall_2011%29/Johnson-Lindenstrauss_Theorem" rel="nofollow">Johnson-Lindenstrauss lemma</a>. I have search for the pseudocode here but could not get any.</p>
  
  <p>I don't know if I have implemented it correctly or not. I just want you guys who understand the lemma to please check my code for me and advice me as to the correct matlab implementation. </p>
  
  <pre><code>n = 2;
  d = 4;
  k = 2;
  G = rand(n,d);
  epsilon = sqrt(log(n)/k);
  
  % Projection in dim k &lt;&lt; d 
  % Defining P (k x d) 
  P = randn(k,d); 
  
  % Projecting down to k-dim
  proj = P.*G;
  u = proj(:,1); 
  v = proj(:,2); 
  % u = P * G(:,5); 
  % v = P * G(:,36); 
  norm(G(:,1)-G(:,2))^2 * k * (1-epsilon); 
  norm(u - v)^2; 
  norm(G(:,1)-G(:,2))^2 * k * (1+epsilon);
  </code></pre>