当然不是!......或者是吗?我们来做一些测试.
定义x = [10 20 30 40 50].然后,正如预期的那样,以下任何语句都会在Matlab中出现错误(下标索引必须是实数正整数或逻辑):
>> 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)
Run Code Online (Sandbox Code Playgroud)
但是,冒号索引中接受非整数值.以下所有工作都在最近的Matlab版本中,尽管有一个警告(当用作索引时,冒号操作符需要整数操作数).
>> 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
Run Code Online (Sandbox Code Playgroud)
如果冒号表达式包括end:
>> x(1.5:end-2)
ans = …Run Code Online (Sandbox Code Playgroud)