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?

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]]])
```

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.

Licensed under: CC-BY-SA with attribution

Not affiliated with: Stack Overflow