[SciPy-user] array vs matrix, converting code from matlab

Travis Oliphant oliphant at ee.byu.edu
Thu Apr 20 14:28:26 EDT 2006


Gennan Chen wrote:

>Hi! All,
>
>I am also in the same process. And I would like to add one more  
>question:
>
>In Matlab, for a 3D array or matrix, the indexing is a(i,j,k). In  
>numpy, it became a[k-1,i-1,j-1]. Is there any way to make it become
>a[i-1,j-1,k-1]? Or I am doing something wrong here??
>
>  
>

In NumPy, arrays and matrices are by default in  C-contiguous order so 
that the last index varies the fastest.    Matlab is based on Fortran 
originally and defines arrays in Fortran-contiguous order (the first 
index varies the fastest as you walk linearly throught memory).     The 
only time this really matters is if you are interfacing with some 
compiled code.   Otherwise, how you think about indexing is up to you 
and how you define the array.

So,  to make it a[i-1,j-1,k-1] you need to reshape the array from the 
way you defined it in MATLAB.    It really does just depend on how you 
define things.  Perhaps you could give a specific example  so we could 
be more specific on how you would write the same thing in NumPy.

-Travis




More information about the SciPy-User mailing list