Of course not! ...Or does it? Let's do some tests.
Define x = [10 20 30 40 50]
. Then any of the following statements, as expected, gives an error in Matlab (Subscript indices must either be real positive integers or logicals):
>> x(1.2)
>> x(-0.3)
>> x([1.4 2 3])
>> x([1.4 2.4 3.4])
>> x([1.4:4])
>> x(end/2)
However, non-integer values are accepted in colon indices. All of the following work in recent Matlab versions, although with a warning (Integer operands are required for colon operator when used as index).
>> x(1.2:3)
ans =
10 20
>> x(0.4:3)
ans =
10 10 20
>> x(0.6:3)
ans =
10 20 30
>> x(1.2:0.7:5)
ans =
10 20 30 30 40 50
>> x(-0.4:3)
ans =
10 10 20 30
It also works if the colon expression includes end
:
>> x(1.5:end-2)
ans =
20 30
>> x(1.5:end/6:end-1)
ans =
20 20 30 40
On the other hand, the following do not work, and give the same error as above:
>> x(-0.6:2)
>> x(-0.5:2)
The observed behaviour can be summarized as follows:
- Some internal rounding kicks in when a colon index is used. A colon index is an expression of the form
a:b
ora:b:c
. No rounding takes place when the indexing array is a standard array, such as[a b c]
or even[a:b]
or[a:b:c]
. - Rounding is done to the nearest integer, except that numbers between
-0.5
and0.5
are special-cased: they are rounded to1
instead of to0
. Of course, if the integer resulting from the rounding is negative an error occurs.
Similar behaviour is seen in recent versions of Octave, except that:
Apparently, normal rounding to the nearest integer is done, without treating numbers between
-0.5
and0.5
as a special case; and so these give an error:>> x(0.4:3) >> x(-0.4:3)
An error is issued when the non-integer range contains a single value:
x(2.4:4)
works, butx(3.4:4)
doesn't (of course,x([2.4 3.4])
andx(3.4)
don't work either).
Other than this, the results are the same as in Matlab, and a warning is also issued (Non-integer range used as index).
The warnings and the fact that Octave works similarly as Matlab suggest that this is intended behaviour. Is it documented somewhere? Can anyone give more infromation or shed some light on this?
x(0.4:3)
andx(-0.4:3)
. – Adiel