[SciPy-user] concatenate array with number

Zachary Pincus zachary.pincus@yale....
Thu Aug 7 11:57:47 CDT 2008


One way to do this is to wrap the last element in a list, not an array:
numpy.concatenate((x, [x[-1]]))

Perhaps simpler and definitely faster is to use a slice to grab the  
last element as an array:
numpy.concatenate((x, x[-1:]))

The latter is the fastest of the various options, and the most compact.

 >>> timeit y = numpy.concatenate((x, [x[-1]]))
100000 loops, best of 3: 12.5 µs per loop

 >>> timeit y = numpy.concatenate((x, x[-1:]))
100000 loops, best of 3: 2.07 µs per loop

 >>> timeit y = numpy.concatenate((x, numpy.array(x[-1], ndmin=1)))
100000 loops, best of 3: 4.45 µs per loop




On Aug 7, 2008, at 11:37 AM, Nicolas Chopin wrote:

>     Hi list,
> I want to do this:
> x = concatenate( (x,x[-1]) )
> i.e. append to 1d array x its last element.
> However, the only way I managed to do this is:
> x  = concatenate( (x,array(x[-1],ndmin=1)) )
> which is a bit cryptic. (if you remove ndmin, it does not work.)
>
> 1. Is there a better way?
> 2. Could concatenate accept floating point numbers as arguments for  
> convenience?
>
> Thanks in advance,
> Nicolas
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user@scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user



More information about the SciPy-user mailing list