On the indexing order in (numpy) arrays
Robert Kern
robert.kern at gmail.com
Thu Oct 9 15:44:24 EDT 2008
Almar Klein wrote:
> Hi,
>
> I was wondering...
>
> Say we have a np.ndarray A of two dimensions (a grayscale image for
> example). If we want to access x:2, y:3, we have to do A[3,2]. Why is
> the order of x and y reversed?
>
> This is reversed in Matlab too, because Matlab is a matrix package and
> matrix are often used this way. (In Matlab the data is actually stored
> last-dimensions-first too.)
Basically, we want a[i][j] == a[i,j]. Since there is no literal syntax for numpy
arrays, we need to be able to convert from a sequence of sequences to an array.
The indexing needs to correspond between the two.
> I suspect numpy has good reasons to do so too, but they are not clear to
> me. I find myself quite a lot wondering if I have to use (or implement) a
> method with order x-y-z, or the other way around. And I suspect this can
> cause quite a lot of confusion and bugs!
You get used to it, I've found.
> If I make a function to do some image operation in a certain dimension:
> def some_operation(image, dim):
> ....
> Would it make more sense if dim=0 means x, or y?
>
> Can anyone shed some light on why this is and how I can determine which
> order to adopt when I create a function like the one above?
Adopt the numpy order. There are many functions in numpy which take an axis=
argument just like this. axis=0 means "y" in the terminology that you are using.
If you have more numpy questions, please join us on the numpy mailing list.
http://www.scipy.org/Mailing_Lists
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
More information about the Python-list
mailing list