title: creating diagonal matrix with selected elements
body: <p>I have a <code>4x5</code> matrix called <code>A</code> from which I want to select randomly 3 rows, then 4 random columns and then select those elements which coincide in those selected rows and columns so that I have 12 selected elements.Then I want to create a diagonal matrix called <code>B</code> which will have entries either 1 or 0 so that multiplication of that <code>B</code> matrix with reshaped <code>A</code> matrix (<code>20x1</code>) will give me those selected 12 elements of <code>A</code>. </p>
  
  <p>How can I create that <code>B</code> matrix? Here is my code:</p>
  
  <pre><code>A=1:20;
  A=reshape(A,4,5);
  Mr=4;
  Ma=3;
  Na=4;
  Nr=5;
  M=Ma*Mr;
  [S1,S2]=size(A);
  N=S1*S2;
  y2=zeros(size(A));
  k1=randperm(S1);
  k1=k1(1:Ma);
  k2=randperm(S2);
  k2=k2(1:Mr);
  y2(k1,k2)=A(k1,k2);
  </code></pre>