[SciPy-user] Breaking up an array of indices into contiguous/ strided chunks

Anand Patil anand.prabhakar.patil@gmail....
Mon Sep 15 12:03:02 CDT 2008


On Mon, Sep 15, 2008 at 4:55 PM, Anand Patil <
anand.prabhakar.patil@gmail.com> wrote:

> Hi all,
> I have an array of indices that I want to use as a slice, but cvxopt sparse
> matrices don't seem to take fancy slices so I need to take the slice
> manually. If I just do it element-by-element in a for-loop it's extremely
> slow. I know that the array of indices is in ascending order, but that's
> all. Does anyone know an easy way to break it up into strided chunks that
> are as big as possible?
>

I didn't ask the question very well. I'm trying to do

x[i1] = y[i2],

where i1 and i2 are different but they're both in ascending order and I have
reason to believe that they're both regular for long intervals, though the
strides will usually be different. I need to break them up into matching,
strided chunks.

The problem gets weird when, for example,

i1 = [1,2,4,5,7,8,10...]
i2 = [1,2,3,4,5,6,7...]

that is when the stride of i1 'alternates'. In this case the copy could be
done efficiently using

s1 = [slice(1,?,3), slice(2,?,3)]
s2 = [slice(1,?,2), slice(2,?,2)]

for k in (1,2):
   x[s1[k]] = y[s2[k]]

and it seems like some code to do this kind of pattern matching would
already be available. Maybe numpy even does it?

Thanks,
Anand
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/scipy-user/attachments/20080915/553acb43/attachment.html 


More information about the SciPy-user mailing list