title: Fastest way to substitute the entries in a 2D Array with entries from 1D Arrat
body: <p>I have a question here about entry sustitution. Let's say we have a matrix (squared) of a fixed size MATRIX_SIZE (unsorted 2D Array), a list of numbers replacementPolicy and another list of number substitudeNUMBER. We loop over the matrix, and if the entry has the same value as the (first) element in the replacementPolicy, we remember the position i, and substitude this entry with the i-th element in substitudeNUMBER. It sounds a little bit complicated, the code is as follows:</p>
  
  <pre><code>void substitute_entry() {
      // For each entry in the matrix
      for (int column = 0; column &lt; MATRIX_SIZE; ++column) {
          for (int row = 0; row &lt; MATRIX_SIZE; ++row) {
              // Search for the entry in the original number list
              // and replace it with corresponding the element in the substituted number list
              int index = -1;
              for (int i = 0; i &lt; LIST_SIZE; i++) {
                  if (replacementPolicy[i] == MATRIX[row][column]) {
                      index = i;
                  }
              }
  
              MATRIX[row][column] = substitutedNUMBER[index];
          }
      }
  }
  
  </code></pre>
  
  <p>However, I would expect to optimize this code in order to achieve a faster runtime. My first idea is to switch the for loop - first over columns and then over rows, but this does not affect the runtime significantly. My second thought is to use a better algorithm to replace the entries, but unfortunately I mess up when testing. Is there any better way to do so?</p>
  
  <p>Thank you!</p>