# [SciPy-user] repmat

Travis Oliphant oliphant.travis at ieee.org
Tue Dec 6 21:31:53 CST 2005

```Aarre Laakso wrote:

>Hi,
>
>Can anyone give me a clue about how I might implement a fast workalike
>for MATLAB's repmat function?
>
>http://www.mathworks.com/access/helpdesk/help/techdoc/ref/repmat.html
>
>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 think the kronecker product is what you want:

kron(ones(m,n),b)

should be the same as

repmat(b, m,n)

Here is the implementation of kron in scipy.linalg  (full scipy --- I
think it should come over to scipy core, though).

>>> utils.source(scipy.linalg.kron)
In file: /usr/lib/python2.4/site-packages/scipy/linalg/basic.py

def kron(a,b):
"""kronecker product of a and b

Kronecker product of two matrices is block matrix
[[ a[ 0 ,0]*b, a[ 0 ,1]*b, ... , a[ 0 ,n-1]*b  ],
[ ...                                   ...   ],
[ a[m-1,0]*b, a[m-1,1]*b, ... , a[m-1,n-1]*b  ]]
"""
if not a.flags['CONTIGUOUS']:
a = reshape(a, a.shape)
if not b.flags['CONTIGUOUS']:
b = reshape(b, b.shape)
o = outerproduct(a,b)
o=o.reshape(a.shape + b.shape)
return concatenate(concatenate(o, axis=1), axis=1)

-Travis

```