title: How to sort the columns of a matrix in order of some other vector in MATLAB?
body: <p>Say I have a vector <code>A</code> of item IDs:</p>
  
  <pre><code>A=[50936
     332680
     107430
     167940
     185820
     99732
     198490
     201250
     27626
     69375];
  </code></pre>
  
  <p>And I have a matrix <code>B</code> whose rows contains values of 8 parameters for each of the items in vector <code>A</code>:</p>
  
  <pre><code>B=[0    0   0   0   0   0   0   0   0   0
     0    0   0   0   0   0   0   0   0   0
     1    0   1   0   0   1   0   1   1   1
     1    0   1   0   0   1   0   1   1   1
     0    0   1   0   0   0   0   1   0   1
     0    0   0   0   0   0   0   1   0   0
     0    0   0   0   0   0   0   0   0   0
     0    0   0   0   0   0   0   0   0   1];
  </code></pre>
  
  <p>So, column 1 in matrix <code>B</code> represents data of item in row 1 of vector <code>A</code>, column 2 in matrix <code>B</code> represents data of item in row 2 of vector <code>A</code>, and so on. However, I want matrix <code>B</code> to contain the information in a different order of items stored in vector <code>A2</code>:</p>
  
  <pre><code>A2=[185820
      198490
      69375
      167940
      99732
      332680
      27626
      107430
      50936
      201250];
  </code></pre>
  
  <p>How do I sort them, so that column 1 of matrix <code>B</code> contains data for item in row 1 of vector <code>A2</code>, column 2 of matrix <code>B</code> contains data for item in row 2 of vector <code>A2</code>, and so on?</p>
  
  <p>My extremely crude solution to do this is the following:</p>
  
  <pre><code>A=A'; A2=A2';
  
  for i=1:size(A,2)
      A(2:size(B,1)+1,i)=B(:,i);
  end
  
  A2(2:size(B,1)+1,:)=zeros(size(B,1),size(B,2));
  
  for i=size(A2,2)
      for j=size(A,2)
          if A2(1,i)==A(1,j)
              A2(2:end,i)=A(2:end,j);
          end
      end
  end
  
  B2 = A2(2:end,:);
  </code></pre>
  
  <p>But I would like to know a cleaner, more elegant and less time consuming method to do this.</p>