Quick numpy array extension
<p>I wonder if there is a trick to extending a numpy array with consecutive
numbers in between each original values, up to a user controlled default length. Perhaps there is already a built-in function that turns x into y</p>

<pre><code>x=np.array([4,8,4,10])

y=np.array([4,5,6,7,8,9,10,11,4,5,6,7,10,11,12,13])
</code></pre>

<p>Here the length between each element that I added was three. Speed is of the essence here. I need something like this for my column data to work in creating a sparse
matrix.</p>

<p>Also, if I have an array such as </p>

<pre><code>s=np.array([0,1])
</code></pre>

<p>is there a quick way to extend each element an arbitrary amount of
times, lets say 4 for example:</p>

<pre><code>s=np.array([0,0,0,0,1,1,1,1])
</code></pre>

<p>I don't know a built-in function that does precisely that, but with some creativity you could do:</p>

<pre><code>&gt;&gt;&gt; x=np.array([4,8,4,10])
&gt;&gt;&gt; np.array([x+i for i in range(4)]).T.ravel()
array([ 4,  5,  6,  7,  8,  9, 10, 11,  4,  5,  6,  7, 10, 11, 12, 13])
</code></pre>

<p>For the second half of your question, have a look at <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.repeat.html#numpy.repeat" rel="nofollow"><code>numpy.repeat</code></a> and <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.tile.html#numpy.tile" rel="nofollow"><code>numpy.tile</code></a>.</p>

python arrays numpy