title: replace indices in matrix with other values in matlab
body: <p>Suppose now I have a matrix</p>
  
  <pre><code>S = [1 1 1 2 2 2;
       1 1 1 2 2 2;
       2 2 2 2 1 1;
       2 2 2 2 1 1;
       2 2 2 2 1 1]
  </code></pre>
  
  <p>And another matrix</p>
  
  <pre><code>A = [1 2;
       2 4]
  </code></pre>
  
  <p>The first row in A is the unique indices of S, and the second row contains the values that the values in the first row will be replaced. That is, all "1"s in S will be replaced by 2, and all "2"s will be replaced by 4. Finally I'll get a matrix</p>
  
  <pre><code>SS = [2 2 2 4 4 4;
        2 2 2 4 4 4;
        4 4 4 4 2 2;
        4 4 4 4 2 2;
        4 4 4 4 2 2]
  </code></pre>
  
  <p>Right now what I'm doing is:</p>
  
  <pre><code>SS = zeros(size(S));
  for i = 1:size(A,2)
      SS(S==index(A(1, i)) = A(2,i);
  end
  </code></pre>
  
  <p>Now, I have a pretty big matrix, and using a for loop is a little bit slow. Is there a faster way to do that?</p>