I found repmat([1;1], [1 1 1])
in MATLAB code.
I am trying to find a NumPy command to do the same thing, but I found that can't use with 3d [1 1 1] ?
I found repmat([1;1], [1 1 1])
in MATLAB code.
I am trying to find a NumPy command to do the same thing, but I found that can't use with 3d [1 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.
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]]])
Know both tile
and repeat
.
x = numpy.arange(5)
print numpy.tile(x, 2)
print x.repeat(2)