Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to cleanly index numpy arrays with arrays (or anything else that supports addition so that it can be offset)
    primarykey
    data
    text
    <p>The easiest way to explain my question may be with an example, so let me define some arrays:</p> <pre><code>&gt;&gt;&gt; test = arange(25).reshape((5,5)) &gt;&gt;&gt; test array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24]]) &gt;&gt;&gt; Xinds = array([1,2,3]) &gt;&gt;&gt; Yinds = array([1,2,3]) </code></pre> <p>Now, if I wanted the elements in rows 1, 2, and 3 and in column 0, I could go:</p> <pre><code>&gt;&gt;&gt; test[Yinds,0] array([ 5, 10, 15]) </code></pre> <p>If I wanted the items in rows 1, 2, and 3 and all columns, I could go:</p> <pre><code>&gt;&gt;&gt; test[Yinds, :] array([[ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19]]) </code></pre> <p>However, if I try to extend this to get the elements in rows 1, 2, and 3 and columns 1, 2, and 3, -- surprise! -- I instead get the elements in (1,1), (2,2), and (3,3)</p> <pre><code>&gt;&gt;&gt; test[Yinds, Xinds] array([ 6, 12, 18]) </code></pre> <p>Instead of what I want:</p> <pre><code>&gt;&gt;&gt; test[Yinds, :][:, Xinds] array([[ 6, 7, 8], [11, 12, 13], [16, 17, 18]]) &gt;&gt;&gt; test[1:4,1:4] array([[ 6, 7, 8], [11, 12, 13], [16, 17, 18]]) </code></pre> <p>I realize I could define a slice, but I want to be able to add an offset to the indices (e.g. Yinds+offset), and that can't be done with slices.</p> <p>I could do something like</p> <pre><code>&gt;&gt;&gt; xStart = 1 &gt;&gt;&gt; xEnd = 4 &gt;&gt;&gt; yStart = 1 &gt;&gt;&gt; yEnd = 4 &gt;&gt;&gt; offset = 1 &gt;&gt;&gt; test[yStart+offset:yEnd+offset, xStart+offset:xEnd+offset] ... </code></pre> <p>or</p> <pre><code>&gt;&gt;&gt; Xinds = array([1,4]) &gt;&gt;&gt; Yinds = array([1,4]) &gt;&gt;&gt; offset = 1 &gt;&gt;&gt; test[slice(*(Yinds+offset)), slice(*(Xinds+offset))] ... </code></pre> <p>But neither is particular clean.</p> <p>Monkey patching the addition operator in to slice doesn't seem to be an option, and inheriting from slice to add the operator doesn't appear to work either; I get the error, "type 'slice' is not an acceptable base type". (*Grumble* This wouldn't be a problem in Ruby *Grumble*)</p> <p>So, my question is, what's the cleanest way to access a (more than 1 dimensional) sub-array with something that can be stored and offset?</p> <p>Options so far:</p> <ul> <li>test[Yinds+offset, :][:, Xinds+offset]</li> <li>test[yStart+offset:yEnd+offset, xStart+offset:xEnd+offset]</li> <li>test[slice(*(Yinds+offset)), slice(*(Xinds+offset))]</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload