jrjohansson / scientific-python-lectures

Lectures on scientific computing with python, as IPython notebooks.
3.51k stars 1.8k forks source link

Indexing nd arrays #13

Open alexjaw opened 10 years ago

alexjaw commented 10 years ago

Great notes! For those of us coming from Matlab trying to learn numpy, i.e. lecture 2. Everything seems to be easy for vectors and 2D arrays. However, an example with 3D shows a big difference compared to Matlab, which maybe should be highlighted (as the normal 0-indexing).

Say we construct the following 3D array m = np.arange(8).reshape(2,2,2)

Asking a Matlab person to get the first items (upper left) from the 2 dimensions would result in something like (knowing that indexing starts at 0...) m[0,0,0] #first item in the first dimension m[0,0,1] #first item in the second dimension

Expecting the numbers 0 and 4. But the result from the above is 0 and 1, respectively. Quite confusing for someone used to Matlab arrays.

Some clarification on accessing elements i higher dimensional arrays would be of great value.