[SciPy-user] repmat

Aarre Laakso aarre at pair.com
Tue Dec 6 19:59:28 CST 2005


Can anyone give me a clue about how I might implement a fast workalike
for MATLAB's repmat function?


The simple use case B = repmat(A,m,n) where A is an array and m,n are
positive integers would be sufficient for my needs.

I have pasted below my own attempt, which obviously involves way too
much copying, because it takes a couple of minutes to replicate a
50-element row vector roughly 5000 times. It seems like repeat with some
tricky slicing might work, but I can't figure out how to do it. Or maybe
there's a better way?

Thanks for any help you can offer.

Aarre Laakso

def repmat(A, *args):
    """ Replicate and tile an array. """

    result = A
    new_size = args
    for axis in range(0, len(new_size)):
        copy_value = result
        num_copies = new_size[axis]
        for curr_copy in range(1, num_copies):
            result = concatenate((result, copy_value), axis)

    return result

More information about the SciPy-user mailing list