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))

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Is the C-contiguous the default order argument.
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.

4 Comments

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 you need Fortran layout when you are using for example BLAS routines programmed in Fortran, so this layout can be very useful
@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 you can have a look at this other answer 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() when calling from Cython

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.