What is the equivalent of MATLAB's repmat in NumPy

后端 未结 7 1509
心在旅途
心在旅途 2020-11-29 18:43

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?

相关标签:
7条回答
  • 2020-11-29 19:17
    >>> import numpy as np
    
    >>> np.repeat(['a','b'], [2,5])
    
    array(['a', 'a', 'b', 'b', 'b', 'b', 'b'], dtype='<U1')
    
    >>> np.repeat([1,2], [2,5])
    
    array([1, 1, 2, 2, 2, 2, 2])
    
    >>> np.repeat(np.array([1,2]), [3]).reshape(2,3)
    
    array([[1, 1, 1],
           [2, 2, 2]])
    
    >>> np.repeat(np.array([1,2]), [2,4]).reshape(3,2)
    
    array([[1, 1],
           [2, 2],
           [2, 2]])
    
    >>> np.repeat(np.matrix('1 2; 3 4'), [2]).reshape(4,2)
    
    matrix([[1, 1],
            [2, 2],
            [3, 3],
            [4, 4]])
    
    0 讨论(0)
提交回复
热议问题