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

David Cournapeau david at ar.media.kyoto-u.ac.jp
Thu Apr 20 22:27:25 CDT 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??
    To be more specific, I am trying to convert a function which compute 
multivariate Gaussian densities. It should be able to handle scalar 
case, the case where the mean is a vector, and the case where va is a 
vector (diagonal covariance matrix) or square matrix (full covariance 
    So, in matlab, I simply do:

function [n, d, K, varmode] = gaussd_args(data, mu, var)

[n, d]  = size(data);
[dm0, dm1] = size(mu);
[dv0, dv1]= size(var);

And I check that the dimensions are what I expect afterwards. Using 
arrays, I don't see a simple way to do that while passing scalar 
arguments to the functions. So either I should be using matrix type (and 
using asmatrix on the arguments), or I should never pass scalar to the 
function, and always pass arrays. But maybe I've used matlab too much, 
and there is a much simpler way to do that in scipy.
    To sum it up, what is the convention in scipy when a function 
handles both scalar and arrays ? Is there an idiom to treat scalar and 
arrays of size 1 the same way, whatever the number of dimensions arrays 
may have ?


More information about the SciPy-user mailing list