Nicolas Chopin | 7 Aug 17:34
Favicon

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

_______________________________________________
SciPy-user mailing list
SciPy-user <at> scipy.org
http://projects.scipy.org/mailman/listinfo/scipy-user
Bing | 7 Aug 18:34

Re: concatenate array with number

try this:
  numpy.r_[x,x[-1]]

On Thu, Aug 7, 2008 at 11:37 AM, Nicolas Chopin <nicolas.chopin <at> 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


_______________________________________________
SciPy-user mailing list
SciPy-user <at> scipy.org
http://projects.scipy.org/mailman/listinfo/scipy-user


_______________________________________________
SciPy-user mailing list
SciPy-user <at> scipy.org
http://projects.scipy.org/mailman/listinfo/scipy-user
Zachary Pincus | 7 Aug 18:57
Favicon

Re: concatenate array with number

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 <at> scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
Nicolas Chopin | 9 Aug 12:19
Favicon

Re: concatenate array with number

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
 

_______________________________________________
SciPy-user mailing list
SciPy-user <at> scipy.org
http://projects.scipy.org/mailman/listinfo/scipy-user

Gmane