How to replace each array element by 4 copies in Python?
<p>How do I use numpy / python array routines to do this ?</p>

<p>E.g. If I have array <code>[ [1,2,3,4,]]</code> , the output should be </p>

<pre><code>[[1,1,2,2,],
 [1,1,2,2,],
 [3,3,4,4,],
 [3,3,4,4]]
</code></pre>

<p>Thus, the output is array of double the row and column dimensions. And each element from original array is repeated three times. </p>

<p>What I have so far is this</p>

<pre><code>def operation(mat,step=2):
    result = np.array(mat,copy=True)
    result[::2,::2] = mat
    return result
</code></pre>

<p>This gives me array </p>

<pre><code>[[ 98.+0.j   0.+0.j  40.+0.j   0.+0.j]
 [  0.+0.j   0.+0.j   0.+0.j   0.+0.j]
 [ 29.+0.j   0.+0.j  54.+0.j   0.+0.j]
 [  0.+0.j   0.+0.j   0.+0.j   0.+0.j]]
</code></pre>

<p>for the input</p>

<pre><code>[[98 40]
 [29 54]]
</code></pre>

<p>The array will always be of even dimensions.</p>

<p>Use <code>np.repeat()</code>:</p>

<pre><code>In [9]: A = np.array([[1, 2, 3, 4]])
In [10]: np.repeat(np.repeat(A, 2).reshape(2, 4), 2, 0)
Out[10]: 
array([[1, 1, 2, 2],
       [1, 1, 2, 2],
       [3, 3, 4, 4],
       [3, 3, 4, 4]])
</code></pre>

<p><strong><em>Explanation:</em></strong> </p>

<p>First off you can repeat the arrya items:</p>

<pre><code>  In [30]: np.repeat(A, 3)
  Out[30]: array([1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4])
</code></pre>

<p>then all you need is reshaping the result (based on your expected result this can be different):</p>

<pre><code>  In [32]: np.repeat(A, 3).reshape(2, 3*2)
  array([[1, 1, 1, 2, 2, 2],
         [3, 3, 3, 4, 4, 4]])
</code></pre>

<p>And now you should repeat the result along the the first axis:</p>

<pre><code>  In [34]: np.repeat(np.repeat(A, 3).reshape(2, 3*2), 3, 0)
  Out[34]: 
  array([[1, 1, 1, 2, 2, 2],
         [1, 1, 1, 2, 2, 2],
         [1, 1, 1, 2, 2, 2],
         [3, 3, 3, 4, 4, 4],
         [3, 3, 3, 4, 4, 4],
         [3, 3, 3, 4, 4, 4]])
</code></pre>

python arrays numpy