beginner question: rank-1 arrays
![](https://secure.gravatar.com/avatar/ac4e2152839c56c5326cd95ae54296e9.jpg?s=120&d=mm&r=g)
Hi, I am trying to implement a project in scipy. I think I am getting somewhere finally. However in my code (I am converting from MATLAB) it is important to maintain 2d arrays, and keep the difference between row and column vectors. After working through some initial problems I think I am getting more of a picture of how things work in numpy. However I am finding my code littered with things like: np.array(np.r_[0:nterms],ndmin=2) (for a row vector) or np.array(np.r_[0:nterms],ndmin=2).T (for a column vector) Coming from matlab and being use to 0:10 for row or (0:10)' for column this seems a bit messy. Is there a better way of constructing row/column 2d arrays from a slice type range? Thanks, Robin
![](https://secure.gravatar.com/avatar/ac4e2152839c56c5326cd95ae54296e9.jpg?s=120&d=mm&r=g)
On 10/8/07, Gael Varoquaux <gael.varoquaux@normalesup.org> wrote:
Thanks, but not really :) Firstly - for me I don' see any difference between the two, they both give a numpy rank-1 array which doesn't have a row/column characteristic. x.shape for both of the above is (10,) I need (10,1) or (1,10) for my code. Robin
![](https://secure.gravatar.com/avatar/5c9fb379c4e97b58960d74dcbfc5dee5.jpg?s=120&d=mm&r=g)
On Mon, Oct 08, 2007 at 11:12:07PM +0100, Robin wrote:
On 10/8/07, Gael Varoquaux <[1]gael.varoquaux@normalesup.org> wrote:
r_[0:10] and c_[0:10].
Thanks, but not really :)
Damn it. Shame on me. I meant c_[0:10,]. If you really need a shape of (1,10) (I have never had such a need) you can use c_[0:10,].T. HTH, Gaël
![](https://secure.gravatar.com/avatar/ac4e2152839c56c5326cd95ae54296e9.jpg?s=120&d=mm&r=g)
On 10/8/07, Gael Varoquaux <gael.varoquaux@normalesup.org> wrote:
Damn it. Shame on me. I meant c_[0:10,]. If you really need a shape of (1,10) (I have never had such a need) you can use c_[0:10,].T.
Thanks! - the trick with the , is just the sort of thing I was looking for - I knew there must be an easy way... Cheers Robin
![](https://secure.gravatar.com/avatar/2b8a71c6c4c0df90de065ce45ee9df33.jpg?s=120&d=mm&r=g)
Alan G Isaac schrieb:
Robin, Alan is right, you want numpy matrices which are always 2d. Check out numpy.matlib; if you replace from numpy import [whatever] by from numpy.matlib import [whatever] you get everything there is in numpy, and things like ones() zeros() empty() etc. will always be 2d matrices. -sven
![](https://secure.gravatar.com/avatar/60dbd4d3241acfcdd4dab5d51a02b9d7.jpg?s=120&d=mm&r=g)
On Nov 16, 2007 5:55 PM, Emanuel Woiski <woiski@gmail.com> wrote:
cols. Mind you, if you slice along a col, you end up with a row - just try it and see. But how can you evaluate an expression such as a[i] - b[j], for all (i,j), with i for rows and j for cols? The trick here is 'newaxis' . With 'newaxis' you have a temporary dimension for a or b, without actually changing a or b shapes. Following the usual meaning, the expression becomes: c = a[:,newaxis] - b See:
That's exactly the same as the one-liner:
Nice isn't it? Hope that help you somehow....:) cheers woiski
![](https://secure.gravatar.com/avatar/ac4e2152839c56c5326cd95ae54296e9.jpg?s=120&d=mm&r=g)
On 10/8/07, Gael Varoquaux <gael.varoquaux@normalesup.org> wrote:
Thanks, but not really :) Firstly - for me I don' see any difference between the two, they both give a numpy rank-1 array which doesn't have a row/column characteristic. x.shape for both of the above is (10,) I need (10,1) or (1,10) for my code. Robin
![](https://secure.gravatar.com/avatar/5c9fb379c4e97b58960d74dcbfc5dee5.jpg?s=120&d=mm&r=g)
On Mon, Oct 08, 2007 at 11:12:07PM +0100, Robin wrote:
On 10/8/07, Gael Varoquaux <[1]gael.varoquaux@normalesup.org> wrote:
r_[0:10] and c_[0:10].
Thanks, but not really :)
Damn it. Shame on me. I meant c_[0:10,]. If you really need a shape of (1,10) (I have never had such a need) you can use c_[0:10,].T. HTH, Gaël
![](https://secure.gravatar.com/avatar/ac4e2152839c56c5326cd95ae54296e9.jpg?s=120&d=mm&r=g)
On 10/8/07, Gael Varoquaux <gael.varoquaux@normalesup.org> wrote:
Damn it. Shame on me. I meant c_[0:10,]. If you really need a shape of (1,10) (I have never had such a need) you can use c_[0:10,].T.
Thanks! - the trick with the , is just the sort of thing I was looking for - I knew there must be an easy way... Cheers Robin
![](https://secure.gravatar.com/avatar/2b8a71c6c4c0df90de065ce45ee9df33.jpg?s=120&d=mm&r=g)
Alan G Isaac schrieb:
Robin, Alan is right, you want numpy matrices which are always 2d. Check out numpy.matlib; if you replace from numpy import [whatever] by from numpy.matlib import [whatever] you get everything there is in numpy, and things like ones() zeros() empty() etc. will always be 2d matrices. -sven
![](https://secure.gravatar.com/avatar/60dbd4d3241acfcdd4dab5d51a02b9d7.jpg?s=120&d=mm&r=g)
On Nov 16, 2007 5:55 PM, Emanuel Woiski <woiski@gmail.com> wrote:
cols. Mind you, if you slice along a col, you end up with a row - just try it and see. But how can you evaluate an expression such as a[i] - b[j], for all (i,j), with i for rows and j for cols? The trick here is 'newaxis' . With 'newaxis' you have a temporary dimension for a or b, without actually changing a or b shapes. Following the usual meaning, the expression becomes: c = a[:,newaxis] - b See:
That's exactly the same as the one-liner:
Nice isn't it? Hope that help you somehow....:) cheers woiski
participants (6)
-
Alan G Isaac
-
Emanuel Woiski
-
Gael Varoquaux
-
Robin
-
Stefan van der Walt
-
Sven Schreiber