[Numpy-discussion] repmat

Stefan van der Walt stefan at sun.ac.za
Fri Oct 6 04:46:01 EDT 2006


On Fri, Oct 06, 2006 at 10:30:43AM +0900, Bill Baxter wrote:
> If this is somehow controversial for some reason then let's discuss
> it.  But so far the only response has been "copying data is a bad
> idea", which is really a separate issue.

An interesting issue, though.  I've often wondered about an array
view where data is visible in more than one place.  For example, you
have an array

x = N.arange((100))

on which you'd like to apply a filter, which, say, finds the moving
variance of the 5 closest elements.  Here, a new representation of x
would be useful:

In [23]: indices = N.arange(5) + N.arange(96).reshape((96,1))

In [24]: indices
Out[24]: 
array([[ 0,  1,  2,  3,  4],
       [ 1,  2,  3,  4,  5],
       ...
       [95, 96, 97, 98, 99]])


In [25]: y = x.index_view(indices) # returns array with same values as
   	                           # y = x[indices]
                                   # except that no values are copied

In [29]: y
Out[29]: 
array([[ 0,  1,  2,  3,  4],
       [ 1,  2,  3,  4,  5],
       [ 2,  3,  4,  5,  6],
       ...

after which you would be able to do

In [30]: y.var(axis=1)
Out[30]: 
array([ 2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.,
        2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.,

While there would be a lot of jumping around in indices, at the loops
can be done in C, which should speed up the process.  Of course, with
small arrays I can simply copy the data, but it becomes troublesome
with images and other such large signals.

I am not sure if providing the indices is the best way to initialise
such an array (otherwise maybe a rule for index calculation?) or how
feasible it would be to implement, so I'd be glad to hear anyone's
thoughts on the topic.

Regards
Stéfan




More information about the NumPy-Discussion mailing list