<br><br><div class="gmail_quote">On Wed, Feb 15, 2012 at 2:25 AM, Steve Schmerler <span dir="ltr"><<a href="mailto:elcortogm@googlemail.com">elcortogm@googlemail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

Hi<br>
<br>
I'd like to repeat an array along a new axis (like broadcast):<br>
<br>
    In [8]: a<br>
    Out[8]:<br>
    array([[0, 1, 2],<br>
           [3, 4, 5]])<br>
    In [9]: b=repeat(a[None,...], 3, axis=0)<br>
    In [10]: b<br>
    Out[10]:<br>
    array([[[0, 1, 2],<br>
            [3, 4, 5]],<br>
<br>
           [[0, 1, 2],<br>
            [3, 4, 5]],<br>
<br>
           [[0, 1, 2],<br>
            [3, 4, 5]]])<br>
<br>
    In [18]: id(a); id(b[0,...]); id(b[1,...]); id(b[2,...])<br>
    Out[18]: 40129600<br>
    Out[18]: 39752080<br>
    Out[18]: 40445232<br>
    Out[18]: 40510272<br>
<br>
<br>
Can I do this such that each sub-array b[i,...] is a view and not a copy?<br></blockquote><div><br><br><br>Yes, such an array can be created using the as_strided() function from the module numpy.lib.stride_tricks:<br><br>

<br><span style="font-family:courier new,monospace">In [1]: from numpy.lib.stride_tricks import as_strided</span><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">In [2]: a = array([[1,2,3],[4,5,6]])</span><br style="font-family:courier new,monospace">

<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">In [3]: b = as_strided(a, strides=(0, a.strides[0], a.strides[1]), shape=(3, a.shape[0], a.shape[1]))</span><br style="font-family:courier new,monospace">

<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">In [4]: b</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">Out[4]: </span><br style="font-family:courier new,monospace">

<span style="font-family:courier new,monospace">array([[[1, 2, 3],</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">        [4, 5, 6]],</span><br style="font-family:courier new,monospace">

<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">       [[1, 2, 3],</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">        [4, 5, 6]],</span><br style="font-family:courier new,monospace">

<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">       [[1, 2, 3],</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">        [4, 5, 6]]])</span><br style="font-family:courier new,monospace">

<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">In [5]: a[0,0] = 99</span><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">In [6]: b</span><br style="font-family:courier new,monospace">

<span style="font-family:courier new,monospace">Out[6]: </span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">array([[[99,  2,  3],</span><br style="font-family:courier new,monospace">

<span style="font-family:courier new,monospace">        [ 4,  5,  6]],</span><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">       [[99,  2,  3],</span><br style="font-family:courier new,monospace">

<span style="font-family:courier new,monospace">        [ 4,  5,  6]],</span><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">       [[99,  2,  3],</span><br style="font-family:courier new,monospace">

<span style="font-family:courier new,monospace">        [ 4,  5,  6]]])</span><br><br><br>Warren<br><br><br></div></div>