[Numpy-discussion] RE: repmat equivalent

Alex Liberzon alex.liberzon at gmail.com
Thu Feb 23 16:48:03 EST 2006


Another thought:

def repmat(a,m,n):
   from scipy import hstack, vstack
   a = eval('hstack(('+n*'a,'+'))')
   return eval('vstack(('+m*'a,'+'))')

may hstack() and vstack() be better for 1d arrays?

>>> from scipy import *
numerix Numeric 24.2
>>> a  = array([1,2])
>>> repmat(a,2,3)
array([[1, 2, 1, 2, 1, 2],
      [1, 2, 1, 2, 1, 2]])
>>> equal(repmat(1,1,1),1)
array([       [1]],'b')

of course, scipy.linalg.kron(ones((m,n,p,...)),a) is more robust and
works for higher dimensions. probably it's the best.




More information about the NumPy-Discussion mailing list