[Numpy-discussion] force column vector
Stefan van der Walt
Wed Feb 7 08:03:50 CST 2007
On Wed, Feb 07, 2007 at 10:35:14AM +0000, Christian wrote:
> when creating an ndarray from a list, how can I force the result to be
> 2d *and* a column vector? So in case I pass a nested list, there will be no
> modification of the shape and when I pass a simple list, it will be
> converted to a 2d column vector. I can only think of a solution using 'if'
> clauses but I suppose there is a more elegant way.
One way is to sub-class ndarray:
import numpy as N
data = N.asarray(data).view(cls)
if len(data.shape) == 1:
data.shape = (-1,1)
x = ColumnVectorArray([[1,2],[3,4],[5,6]])
print 'x ='
y = ColumnVectorArray([1,2,3])
print 'y ='
print 'x+y ='
More information about the Numpy-discussion