title: How to retain only those rows in a matrix corresponding to the values in another matrix?
body: <p>I have a m x n matrix A where both m and n are very large values and another column matrix B(which has duplicate values) that contains some row values. I need to create a new matrix that retains only those rows in A that are present in B in that order and discard the rest of them. </p>
  
  <p>Example:</p>
  
  <pre><code>A = [2 5 6;
       7 3 9;
      -6 4 5;
       6 7 3];
  
  B = [2;
       4;
       2;
       1]; 
  </code></pre>
  
  <p>The output C should contain the rows of A as follows:</p>
  
  <pre><code>C = [7 3 9;
       6 7 3;
       7 3 9;
       2 5 6];
  </code></pre>
  
  <p>I've used this loop and tried to do it without the need for a new matrix C but it's erroneous.</p>
  
  <pre><code>for n = 1:length(B)
     A(~B(n),:)=[];
  end
  </code></pre>
  
  <p>Does the solution to the above work the same on a variable if it isn't a matrix but an object?</p>