Which is equivalent to MATLAB repmat in NumPy

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

+72
python numpy matlab
Nov 12 '09 at 12:20
source share
5 answers

Here's a much better (official) NumPy link for Matlab users - I'm afraid that one of them is out of date.

Zero equivalent of repmat(a, m, n) tile(a, (m, n)) .

This works with multiple dimensions and gives a similar result for matlab. (Numpy gives a three-dimensional output array, as one 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]]]) 
+78
Nov 12 '09 at 18:36
source share

Please note that some of the reasons why you need to use MATLAB repmat will be taken care of by the NumPy broadcasting mechanism, which allows you to do different types of math with arrays of a similar shape. So if you had, say, an 1600x1400x3 array representing a three-color image, you could (differently) multiply it by [1.0 0.25 0.25] to reduce the amount of green and blue on each pixel. See the link above for more information.

+14
Nov 12 '09 at 13:24
source share

See NumPy for Matlab users .

Matlab:

 repmat(a, 2, 3) 

Numpy:

 numpy.kron(numpy.ones((2,3)), a) 
+8
Nov 12 '09 at 13:09
source share

Know both tile and repeat .

 x = numpy.arange(5) print numpy.tile(x, 2) print x.repeat(2) 
+4
Jan 19 '10 at 23:52
source share

That's how I understood it because he was messing around. Thank you for the fix and hope this helps.

Say you have an M matrix of 2x3 elements. Obviously, this has two dimensions.




I do not see the difference between Matlab and Python, asking me to manipulate the input matrix in the sizes that the matrix already has. So the two teams

 repmat(M,m,n) % matlab np.tile(M,(m,n)) # python 

really equivalent for a rank 2 matrix (two dimensions).




Questions contradict intuition when you request repetition / splitting into several dimensions than the input matrix. Returning to the second-rank matrix M and the 2x3 shape, just look at what happens to the size / shape of the output matrix. Let's say that the sequence of manipulations is now 1,1,2.

In matlab

 > size(repmat(M,1,1,2)) ans = 2 3 2 

he copied the first two dimensions (rows and columns) of the input matrix and repeated it once to a new third dimension (it is copied twice, that is). True for naming repmat for rematrix.

In python

 >>> np.tile(M,(1,1,2)).shape (1, 2, 6) 

he applied a different procedure because, I believe, the sequence (1,1,2) is read differently than in Matlab. The number of copies in the direction of columns, rows, and dimension outside the plane is read from right to left. The resulting object has a different form from Matlab. It can no repmat be argued that repmat and tile are equivalent instructions.




To make tile behave like repmat , in Python you need to make sure that the input matrix has the same size as the elements in the sequence. This is done, for example, by a small precondition and the creation of a related object N

 N = M[:,:,np.newaxis] 

Then on the input side there is N.shape = (2,3,1) , not M.shape = (2,3) , but on the output side

 >>> np.tile(N,(1,1,2)).shape (2, 3, 2) 

which was the answer of size(repmat(M,1,1,2)) . I assume that this is due to the fact that we were guided by Python to add the third dimension to the right of (2,3), and not to the left, so Python develops the sequence (1,1,2), as provided in the Matlab method his reading.

The element in [:,:,0] in the Python answer for N will contain the same values ​​as the element (:,:,1) ,, (:,:,1) Matlab answer for M.




Finally, I cannot find the equivalent for repmat when you use the Kronecker product from

 >>> np.kron(np.ones((1,1,2)),M).shape (1, 2, 6) 

if I did not stipulate condition M in N , as indicated above. Therefore, I would say that the most common way to move on is to use np.newaxis methods.




The game gets harder if we look at a rank 3 matrix L (three dimensions) and the simple case of adding new dimensions to the output matrix. These two seemingly equivalent instructions will not produce the same results.

 repmat(L,p,q,r) % matlab np.tile(L,(p,q,r)) # python 

because the rows, columns, unplanned directions are (p, q, r) in Matlab and (q, r, p) in Python, which were not visible in the ranks of the 2nd level. There you need to be careful, and getting the same results with two languages ​​will require more preliminary training.




I know that this reasoning may well be general, but I could only figure it out. Hopefully this will invite other fellows to test it at a tough level.

+3
Mar 27 '16 at 19:48
source share