title: Tracking Multi-Index and Modifying Values in NumPy
body: <p>I have a 2D array that I am iterating over in an effort to use the index values to make calculations and then assign the calculated value to said index.</p>
  
  <p>In the NumPy documentation, <a href="https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.nditer.html#modifying-array-values" rel="nofollow noreferrer">an example</a> is provided for modifying values using an iterator:</p>
  
  <pre><code>for x in np.nditer(a, op_flags=['readwrite']):
      x[...] = 2 * x
  </code></pre>
  
  <p>However, this doesn't seem to work when tracking an index using <a href="https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.nditer.html#tracking-an-index-or-multi-index" rel="nofollow noreferrer">the following method</a>:</p>
  
  <pre><code>it = np.nditer(a, flags=['multi_index'])
  while not it.finished:
      it[...] = . . .
      it.iternext()
  </code></pre>
  
  <p>I am, however, able to use the <code>it.multi_index</code> values, but it seems unnecessarily verbose. Is there a simpler way to achieve this, either through a different approach or different syntax?</p>
  
  <pre><code>it = np.nditer(a, flags=['multi_index'])
  while not it.finished:
      matrix[it.multi_index[0]][it.multi_index[1]] = . . .
      it.iternext()
  </code></pre>
  
  <hr>
  
  <p><strong>EDIT</strong></p>
  
  <p>Here is an example of a <code>multi_index</code> iteration attempting to modify values using iterator indexing and failing.</p>
  
  <pre><code>matrix = np.zeros((5,5))
  it = np.nditer(matrix, flags=['multi_index'])
  while not it.finished:
    it[...] = 1
    it.iternext()
  </code></pre>
  
  <p>The error produced is</p>
  
  <pre><code>TypeError                                 Traceback (most recent call last)
  &lt;ipython-input-79-3f4cabcbfde6&gt; in &lt;module&gt;()
       25 it = np.nditer(matrix, flags=['multi_index'])
       26 while not it.finished:
  ---&gt; 27   it[...] = 1
       28   it.iternext()
  
  TypeError: invalid index type for iterator indexing
  </code></pre>