Access all elements at given x, y position in 3-dimensional numpy array
<pre><code>mat_a = np.random.random((5, 5))
mat_b = np.random.random((5, 5))
mat_c = np.random.random((5, 5))
bigmat = np.stack((mat_a, mat_b, mat_c)) # this is a 3, 5, 5 array

for (x, y, z), value in np.ndenumerate(bigmat):
    print (x, y, z)
</code></pre>

<p>In the example above, how can I loop so that I iterate only across the 5 x 5 array and at each position I get 3 values i.e. loop should run 25 times and each time, I get an array with 3 values (one from each of mat_a, mat_b and mat_c)</p>

<ul>
<li>EDIT: Please note that I need to be able to access elements by position later i.e. if <code>bigmat</code> is reshaped, there should be a way to access element based on specific y, z</li>
</ul>

<p>If you don't actually need to stack the arrays, and only want to iterate over all three arrays, element-wise, <em>at once</em>, <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.nditer.html" rel="nofollow">numpy.nditer</a> works - I'm still fuzzy on all its parameters  I don't know if it is any faster, test it on a subset.</p>

<pre><code>a1 = np.arange(9).reshape(3,3) + 10
a2 = np.arange(9).reshape(3,3) + 20
a3 = np.arange(9).reshape(3,3) + 30

c = np.nditer((a1, a2, a3))
for thing in c:
    print(np.array(thing))

&gt;&gt;&gt; 
[10 20 30]
[11 21 31]
[12 22 32]
[13 23 33]
[14 24 34]
[15 25 35]
[16 26 36]
[17 27 37]
[18 28 38]
&gt;&gt;&gt;
</code></pre>

python numpy