# What is the equivalent of MATLAB's repmat in NumPy

### Question

I would like to execute the equivalent of the following MATLAB code using NumPy: `repmat([1; 1], [1 1 1])`. How would I accomplish this?

1
93
9/10/2014 1:58:34 AM

Here is a much better (official) NumPy for Matlab Users link - I'm afraid the mathesaurus one is quite out of date.

The numpy equivalent of `repmat(a, m, n)` is `tile(a, (m, n))`.

This works with multiple dimensions and gives a similar result to matlab. (Numpy gives a 3d output array as you would expect - matlab for some reason gives 2d output - but the content is the same).

Matlab:

``````>> repmat([1;1],[1,1,1])

ans =
1
1
``````

Python:

``````In [46]: a = np.array([[1],[1]])
In [47]: np.tile(a, [1,1,1])
Out[47]:
array([[[1],
[1]]])
``````
94
12/29/2018 8:14:34 PM

Note that some of the reasons you'd need to use MATLAB's repmat are taken care of by NumPy's broadcasting mechanism, which allows you to do various types of math with arrays of similar shape. So if you had, say, a 1600x1400x3 array representing a 3-color image, you could (elementwise) multiply it by `[1.0 0.25 0.25]` to reduce the amount of green and blue at each pixel. See the above link for more information.