Does Matlab accept non-integer indices?
<p>Of course not! ...Or does it? Let's do some tests.</p>

<p>Define <code>x = [10 20 30 40 50]</code>. Then any of the following statements, as expected, gives an error in <strong>Matlab</strong> (<em>Subscript indices must either be real positive integers or logicals</em>): </p>

<pre><code>&gt;&gt; x(1.2)
&gt;&gt; x(-0.3)
&gt;&gt; x([1.4 2 3])
&gt;&gt; x([1.4 2.4 3.4])
&gt;&gt; x([1.4:4])
&gt;&gt; x(end/2)
</code></pre>

<p>However, non-integer values are accepted in <strong>colon indices</strong>. All of the following work in recent Matlab versions, although with a warning (<em>Integer operands are required for colon operator when used as index</em>).</p>

<pre><code>&gt;&gt; x(1.2:3)
ans =
    10    20

&gt;&gt; x(0.4:3)
ans =
    10    10    20

&gt;&gt; x(0.6:3)
ans =
    10    20    30

&gt;&gt; x(1.2:0.7:5)
ans =
    10    20    30    30    40    50

&gt;&gt; x(-0.4:3)
ans =
    10    10    20    30
</code></pre>

<p>It also works if the colon expression includes <code>end</code>:</p>

<pre><code>&gt;&gt; x(1.5:end-2)
ans =
    20    30

&gt;&gt; x(1.5:end/6:end-1)
ans =
    20    20    30    40
</code></pre>

<p>On the other hand, the following do not work, and give the same error as above:</p>

<pre><code>&gt;&gt; x(-0.6:2)
&gt;&gt; x(-0.5:2)
</code></pre>

<p>The observed behaviour can be <strong>summarized</strong> as follows:</p>

<ul>
<li>Some <strong>internal rounding</strong> kicks in when a <strong>colon index</strong> is used. A colon index is an expression of the form <code>a:b</code> or <code>a:b:c</code>. No rounding takes place when the indexing array is a standard array, such as <code>[a b c]</code> or even <code>[a:b]</code> or <code>[a:b:c]</code>.</li>
<li>Rounding is done to the <strong>nearest integer</strong>, except that numbers between <code>-0.5</code> and <code>0.5</code> are <strong>special-cased</strong>: they are rounded to <code>1</code> instead of to <code>0</code>. Of course, if the integer resulting from the rounding is negative an error occurs.</li>
</ul>

<p>Similar behaviour is seen in recent versions of <strong>Octave</strong>, except that:</p>

<ul>
<li><p>Apparently, normal rounding to the nearest integer is done, without treating numbers between <code>-0.5</code> and <code>0.5</code> as a special case; and so these give an error:</p>

<pre><code>&gt;&gt; x(0.4:3)
&gt;&gt; x(-0.4:3)
</code></pre></li>
<li><p>An error is issued when the non-integer range contains a single value: <code>x(2.4:4)</code> works, but <code>x(3.4:4)</code> doesn't (of course, <code>x([2.4 3.4])</code> and <code>x(3.4)</code> don't work either).</p></li>
</ul>

<p>Other than this, the results are the same as in Matlab, and a warning is also issued (<em>Non-integer range used as index</em>).</p>

<p>The warnings and the fact that Octave works similarly as Matlab suggest that this is <strong>intended</strong> behaviour. Is it <strong>documented</strong> somewhere? Can anyone give <strong>more infromation</strong> or shed some light on this?</p>