[Numpy-discussion] fastest way to make two vectors into an array

Pearu Peterson pearu at cens.ioc.ee
Thu Jan 30 01:51:09 CST 2003


On Wed, 29 Jan 2003, John Hunter wrote:

> 
> I have two equal length 1D arrays of 256-4096 complex or floating
> point numbers which I need to put into a shape=(len(x),2) array.
> 
> I need to do this a lot, so I would like to use the most efficient
> means.  Currently I am doing:
> 
> def somefunc(x,y):
>     X = zeros( (len(x),2), typecode=x.typecode())
>     X[:,0] = x
>     X[:,1] = y
>     do_something_with(X)
> 
> Is this the fastest way?

May be you could arange your algorithm so that you first create
X and then reference its columns by x,y without copying:

# Allocate memory
X = zeros( (n,2), typecode=.. )

# Get references to columns
x = X[:,0]
y = X[:,1]

while 1:
  do_something_inplace_with(x,y)
  do_something_with(X)

Pearu





More information about the Numpy-discussion mailing list