concatenate array with number
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 <http://www.stats.bris.ac.uk/%7Emanxac/> <http://www.stats.bris.ac.uk/%7Emanxac/>
try this: numpy.r_[x,x[-1]] On Thu, Aug 7, 2008 at 11:37 AM, Nicolas Chopin < nicolas.chopin@bristol.ac.uk> 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 <http://www.stats.bris.ac.uk/%7Emanxac/> <http://www.stats.bris.ac.uk/%7Emanxac/>
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-user
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
sorry, I realise now I should have posted to the numpy mailing list. Many thanks for the answers. My personal favourite is: numpy.concatenate((x, x[-1:])) because it's easier to remember. Too bad x[-1] is not recognised as an array as well (like in Matlab for instance). Best NC
participants (3)
-
Bing
-
Nicolas Chopin
-
Zachary Pincus