NumPy matrix not indexing range correctly [0,0,0,5:53].shape = 43
<p>I have a rather large numpy matrix, and I'm thinking since it is so large this is happening. It has to be some sort of pointer issue.</p>

<p>Anyways, I have the following matrix </p>

<pre><code>(Pdb) input_4d_volume.shape
(26010, 48, 48, 48)
</code></pre>

<p>When I index like the following, no problem: </p>

<pre><code>(Pdb) input_4d_volume[0,0,0,0:48].shape[0]
48
</code></pre>

<p>But when I try to do a range, the max always max's out at <code>48</code></p>

<p>For example, take these two cases:</p>

<pre><code>(Pdb) input_4d_volume[0,0,0,1:1+48].shape[0]
47
(Pdb) input_4d_volume[0,0,0,10:10+48].shape[0]
38
</code></pre>

<p>The expected outputs should be:</p>

<pre><code>(Pdb) input_4d_volume[0,0,0,1:1+48].shape[0]
48
(Pdb) input_4d_volume[0,0,0,10:10+48].shape[0]
38
</code></pre>

<p>Since, <code>(1+48)-1 = 48</code> and <code>(10+48)-10 = 48</code> (see below examples as proof)</p>

<p>As proof this works, I have another matrix that works perfectly fine</p>

<pre><code>(Pdb) entire_volume.shape
(99, 396, 396)
(Pdb) entire_volume[0,0,0:100].shape[0]
100
(Pdb) entire_volume[0,0,10:10+100].shape[0]
100
</code></pre>

<p>Is this because <code>input_4d_volume</code> is too large?</p>

<p>The shape of your numpy array is <code>(26010, 48, 48, 48)</code>, so when you ask for <code>input_4d_volume[0,0,0,n:n+48].shape[0]</code> where <code>n</code> is some positive integer, the answer will be <code>max(48-n,0)</code>. The behaviour you are seeing is correct, because python indexing starts at 0, and accepts indices beyond the end of an array. The numpy docs explain how slicing works: <a href="http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html" rel="nofollow">http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html</a>.</p>

python numpy