# [Numpy-discussion] Complex slicing and take

Eric Emsellem emsellem@obs.univ-lyon1...
Tue Dec 29 09:58:14 CST 2009

```Hi (sorry if you receive this twice, but I did not see the first post appear)

I have a nagging problem which I think could be solved nicely with numpy
indexing but I cannot find the solution, except by invoking a stupid
loop. I would like to do this with some numpy item.

Problem
========
I have a 2D array which is let's say 1000 x 100.
This represents 100 1D spectra, each one containing 1000 datapoints. For
example:

startarray = random((1000,100))

Assuming I have a list of indices, for example:

ind = [1,2,5,6,1,2]

and a corresponding list of integer shifts let's say between -100 and 100:

shift = [10,20,34,-10,22,-20]

I would like to do the following sum

result = stararray[100+shift[0]:-100+shift[0],ind[0]]
+ stararray[100+shift[1]:-100+shift[0],ind[0]]
+ ...

Basically: I would like to sum some extracted 1D line after they have been
shifted by some amount. Note that I would like to be able to use each line
several times (with different shifts).

At the moment, I am using "take" to extract from this array some of these
spectra, so let's start from scratch here:

import numpy as num
startarray = random((1000,100))
take_sample = [1,2,5,6,1,2]
temp = num.take(startarray,take_sample,axis=1)

# and to do the sum after shifting I need a loop

shift = [10,20,34,-10,22,-20]
result = num.zeros(900)  # shorter than initial because of the shift
for i in range(len(shift)) :
result += temp[100+shift[i]:-100+shift[1]]

Is there a way to do this without invoking a loop? Is there also a FAST
solution which makes the shifts at the same time than the "take" (that would
then prevent the use of the "temp" array)?

Of course the arrays I am using are much BIGGER than these, so I really need an
efficient way to do all this.

thanks!

Eric

--

--------------------------------------------------------------------------
Ce message a été envoyé depuis le webmail IMP (Internet Messaging Program)
```