stacking scalars with column vector
Hi , I am trying to learn python to convert some of my mlab codes to python.Am new to python.Some help here would be appreciated I am trying to make a column vector by stacking two scalars-->Xstart,Xend and second column of n X 2 2Darray-->A In matlab the code is b=[Xstart;A(:,1);Xend] I have made the following code import numpy as np x=np.array([[Xstart]]) arr_temp=np.array([A[:,0]]) x=np.vstack((x,arr_temp.T)) x=np.vstack((x,[[Xend]])) This works, but is there a more elegant way of doing this. Also if i define arr_temp as arr_temp=A[:,0], the array arr_temp which is supposed to be of shape (n,1) shows as (n,) .Why is this Thanks Santhosh
You can do it in one shot with: x = np.vstack((Xstart, A[:, 0:1], Xend)) Using A[:, 0:1] instead of A[:, 0] lets you keep it as a 2d matrix (this should answer your last question). Then the scalars Xstart and Xend will automatically be broadcasted to accomodate the shape of A[:, 0:1], so you don't need to write [[Xstart]] and [[Xend]]. -=- Olivier 2011/12/4 Santhosh R <san.programming@gmail.com>
Hi ,
I am trying to learn python to convert some of my mlab codes to python.Am new to python.Some help here would be appreciated
I am trying to make a column vector by stacking two scalars-->Xstart,Xend and second column of n X 2 2Darray-->A
In matlab the code is b=[Xstart;A(:,1);Xend]
I have made the following code
import numpy as np x=np.array([[Xstart]]) arr_temp=np.array([A[:,0]]) x=np.vstack((x,arr_temp.T)) x=np.vstack((x,[[Xend]]))
This works, but is there a more elegant way of doing this. Also if i define arr_temp as arr_temp=A[:,0], the array arr_temp which is supposed to be of shape (n,1) shows as (n,) .Why is this
Thanks
Santhosh
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Thanks Oliver. You can do it in one shot with:
x = np.vstack((Xstart, A[:, 0:1], Xend))
Using A[:, 0:1] instead of A[:, 0] lets you keep it as a 2d matrix (this should answer your last question). Then the scalars Xstart and Xend will automatically be broadcasted to accomodate the shape of A[:, 0:1], so you don't need to write [[Xstart]] and [[Xend]].
-=- Olivier
participants (2)
-
Olivier Delalleau -
Santhosh R