# [SciPy-user] getting rid of for loops...

David Warde-Farley dwf@cs.toronto....
Thu Aug 2 16:11:35 CDT 2007

```On 2-Aug-07, at 5:05 AM, Emanuele Zattin wrote:

> Hello!
>
> say A is an array and N is an integer. Is there any smart (read fast!)
> way to obtain an array C that contains each row of A repeated N times?
>
> What i'm trying to do is to optimize something that looks like this:
>
> close_enough = []
> tollerance = array([10, 15, 5, 8, 6])
> for row_i in data1:
>    for row_j in data2:
>       if less(absolute(row_i - row_j), tollerance).all:
>          close_enough.append([row_i, row_j])
>
> i was thinking that using matrices like the ones described above i
> might be able to get rid of the for loops and (maybe) speed up the
> code (now it takes something like one minute).

If you want to do what I think you want to do, you should be able to
use indexing tricks like the following:

In [7]: m = array([[5,4,3,2,1]])

In [8]: m
Out[8]: array([[5, 4, 3, 2, 1]])

In [9]: m[[0,0,0,0,0],:]
Out[9]:
array([[5, 4, 3, 2, 1],
[5, 4, 3, 2, 1],
[5, 4, 3, 2, 1],
[5, 4, 3, 2, 1],
[5, 4, 3, 2, 1]])

Basically use an array of indices along one dimension that just
repeats the same index over and over again. If NumPy's implementation
is as clever as I think, then it shouldn't even require a copy.

David

```