1

In Matlab the buffer of matrix is continuous in column . So what about the numpy array of Python. which one is beter between numpy.empty((n,1)) and numpy.empty((1,n))

Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234
Samuel
  • 5,977
  • 14
  • 55
  • 77

2 Answers2

1

In numpy you can choose between Fortran-contiguous (along the column, like in Matlab) and C-contiguous (along the row, which is the default in numpy) order, passing the order argument when you create an array, so you have more flexibility.

As @user2357112 already said, for a 1xN or Nx1 array it does not matter, but for a MXN array it does matter and you should be aware of that.

Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234
0

They do different things. One makes an Nx1 array; the other makes a 1xN array. Neither is "better". (In fact, the memory layout will be identical for both arrays, even if you specify column-major storage.)

To answer the question about storage layout, though, numpy defaults to row-major layout, a.k.a. C-contiguous. You can see this clearly reflected in the docs.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • And if you specify fortran-layout, whatever numpy routine you use will still return you something in default order (so fortran-layout seems pretty pointless to me) – usethedeathstar Aug 30 '13 at 06:48
  • @usethedeathstar you need Fortran layout when you are using for example `BLAS` routines programmed in Fortran, so this layout can be very useful – Saullo G. P. Castro Aug 30 '13 at 07:52
  • @SaulloCastro how do you access those routines from numpy? Is there a fortran version of things like numpy.sqrt(x) or whatever you wish to do? Or do you mean if you would work with f2py or so? – usethedeathstar Aug 30 '13 at 10:56
  • 1
    @usethedeathstar you can have a look [at this other answer](http://stackoverflow.com/a/16153914/832621) for futher information... this can considerably speed up your code, but you should worry about this only for heavy tasks...[I adapted this from pv for a faster `dot()`](https://gist.github.com/saullocastro/6388865) when calling from Cython – Saullo G. P. Castro Aug 30 '13 at 11:18