<p dir="auto"><em>Original ticket <a href="http://projects.scipy.org/numpy/ticket/292" rel="nofollow">http://projects.scipy.org/numpy/ticket/292</a> on 2006-09-23 by <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/baxissimo/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/baxissimo">@baxissimo</a>, assigned to unknown.</em></p>
<p dir="auto">repmat() should work with inputs of any shape and take a tuple for the output number of repeats.</p>
<p dir="auto">If this is considered too big a change to replace repmat's 3-argument signature with an incompatible 2-arg one, then a new function 'reparray' should be added.</p>
<p dir="auto">An example implementation is below</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="def reparray(A, tup):
    &quot;&quot;&quot;Repeat an array the number of times given in the integer tuple, tup.

    Similar to repmat, but works for arrays of any dimension.
    reparray(A,(m,n)) is equivalent to repmat(A,m,n)

    If tup has length d, the result will have dimension of max(d, A.ndim).
    If tup is scalar it is treated as a 1-tuple.

    If A.ndim &lt; d, A is promoted to be d-dimensional by prepending new axes.
    So a shape (3,) array is promoted to (1,3) for 2-D replication,
    or shape (1,1,3) for 3-D replication.
    If this is not the desired behavior, promote A to d-dimensions manually
    before calling this function.

    If d &lt; A.ndim, tup is promoted to A.ndim by appending 1's to it.  Thus
    for an A.shape of (2,3,4,5), a tup of (2,2) is treated as (2,2,1,1)


    Examples:
    &gt;&gt;&gt; a = array([0,1,2])
    &gt;&gt;&gt; reparray(a,2)
    array([0, 1, 2, 0, 1, 2])
    &gt;&gt;&gt; reparray(a,(1,2))
    array([[0, 1, 2, 0, 1, 2]])
    &gt;&gt;&gt; reparray(a,(2,2))
    array([[0, 1, 2, 0, 1, 2],
           [0, 1, 2, 0, 1, 2]])
    &gt;&gt;&gt; reparray(a,(2,1,2))
    array([[[0, 1, 2, 0, 1, 2]],

           [[0, 1, 2, 0, 1, 2]]])

    See Also:
       repmat, repeat
    &quot;&quot;&quot;
    if numpy.isscalar(tup):
        tup = (tup,)
    d = len(tup)
    c = numpy.array(A,copy=False,subok=True,ndmin=d)
    shape = list(c.shape)
    n = c.size
    for i, nrep in enumerate(tup):
        if nrep!=1:
            c = c.reshape(-1,n).repeat(nrep,0)
        dim_in = shape[i]
        dim_out = dim_in*nrep
        shape[i] = dim_out
        n /= dim_in
    return c.reshape(shape)"><pre class="notranslate"><code class="notranslate">def reparray(A, tup):
    """Repeat an array the number of times given in the integer tuple, tup.

    Similar to repmat, but works for arrays of any dimension.
    reparray(A,(m,n)) is equivalent to repmat(A,m,n)

    If tup has length d, the result will have dimension of max(d, A.ndim).
    If tup is scalar it is treated as a 1-tuple.

    If A.ndim &lt; d, A is promoted to be d-dimensional by prepending new axes.
    So a shape (3,) array is promoted to (1,3) for 2-D replication,
    or shape (1,1,3) for 3-D replication.
    If this is not the desired behavior, promote A to d-dimensions manually
    before calling this function.

    If d &lt; A.ndim, tup is promoted to A.ndim by appending 1's to it.  Thus
    for an A.shape of (2,3,4,5), a tup of (2,2) is treated as (2,2,1,1)


    Examples:
    &gt;&gt;&gt; a = array([0,1,2])
    &gt;&gt;&gt; reparray(a,2)
    array([0, 1, 2, 0, 1, 2])
    &gt;&gt;&gt; reparray(a,(1,2))
    array([[0, 1, 2, 0, 1, 2]])
    &gt;&gt;&gt; reparray(a,(2,2))
    array([[0, 1, 2, 0, 1, 2],
           [0, 1, 2, 0, 1, 2]])
    &gt;&gt;&gt; reparray(a,(2,1,2))
    array([[[0, 1, 2, 0, 1, 2]],

           [[0, 1, 2, 0, 1, 2]]])

    See Also:
       repmat, repeat
    """
    if numpy.isscalar(tup):
        tup = (tup,)
    d = len(tup)
    c = numpy.array(A,copy=False,subok=True,ndmin=d)
    shape = list(c.shape)
    n = c.size
    for i, nrep in enumerate(tup):
        if nrep!=1:
            c = c.reshape(-1,n).repeat(nrep,0)
        dim_in = shape[i]
        dim_out = dim_in*nrep
        shape[i] = dim_out
        n /= dim_in
    return c.reshape(shape)
</code></pre></div>
<p dir="auto">A more complete version with two implementations and tests and timing code is attached.</p>
<p dir="auto">It seems as argsort has a bug for dimension 3 arrays in an specific order.</p>
<p dir="auto">For instance if I wrote</p>
<p dir="auto">import numpy as np</p>
<p dir="auto">np.argsort(array([2,3,1])</p>
<p dir="auto">The returned value is:<br>
array([2, 0, 1])</p>
<p dir="auto">If I change the order of the numbers (i.e. [1,2,3]) the function works well.</p>
<p dir="auto">I have cross checked the malfunction in python 2.7.6 with numpy 1.8.2 and python 3.5.2 with numpy version 1.11.1 both in windows and linux.</p>