Matlab是否接受非整数索引? [英] Does Matlab accept non-integer indices?

查看:610
本文介绍了Matlab是否接受非整数索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当然不是! ...或者是吗?让我们做一些测试.

Of course not! ...Or does it? Let's do some tests.

定义x = [10 20 30 40 50].然后,以下任何预期的语句都会在 Matlab 中出现错误(下标索引必须是实数正整数或逻辑):

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)

但是,冒号索引中接受非整数值.以下所有工作在最新的Matlab版本中都有,但有警告(将冒号运算符用作索引时,整数操作数是必需的).

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

如果冒号表达式包含end,它也将起作用:

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:

    使用冒号索引时,会出现
  • 一些内部四舍五入.冒号索引是形式为a:ba:b:c的表达式.当索引数组是标准数组(例如[a b c]甚至[a:b][a:b:c])时,不进行舍入.
  • 最近整数进行四舍五入,除了-0.50.5之间的数字为特殊大小写:将它们四舍五入为1,而不是四舍五入到0.当然,如果舍入所得的整数为负,则会发生错误.
  • Some internal rounding kicks in when a colon index is used. A colon index is an expression of the form a:b or a: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 and 0.5 are special-cased: they are rounded to 1 instead of to 0. 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:

  • 显然,可以正常舍入到最接近的整数,而不会将-0.50.5之间的数字作为特殊情况处理.所以这些给出了一个错误:

  • Apparently, normal rounding to the nearest integer is done, without treating numbers between -0.5 and 0.5 as a special case; and so these give an error:

>> x(0.4:3)
>> x(-0.4:3)

  • 当非整数范围包含单个值时,会发出错误:x(2.4:4)有效,但x(3.4:4)无效(当然,x([2.4 3.4])x(3.4)也无效) .

  • An error is issued when the non-integer range contains a single value: x(2.4:4) works, but x(3.4:4) doesn't (of course, x([2.4 3.4]) and x(3.4) don't work either).

    除此之外,结果与Matlab中的结果相同,并且还会发出警告(非整数范围用作索引).

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

    警告和Octave与Matlab相似的事实表明,这是预期的行为.它在某处已记录吗?谁能给出更多信息或对此有所了解?

    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(1.2:3)理论上应解释为:subsref(x, substruct('()',1.2:3)).但是,如问题中所述,当索引数组为标准数组时,不进行舍入",这将导致显式下标引用失败.这表明该机制类似于逻辑短路,或者也许是<发生了href ="https://stackoverflow.com/a/18499272/3372061">多线程分区(其中未真正创建"中间变量).

      Additional observations:

      • x(1.2:3) should theoretically be interpreted as: subsref(x, substruct('()',1.2:3)). However, as mentioned in the question, "No rounding takes place when the indexing array is a standard array", which causes the explicit subscripted reference to fail. This suggests that a mechanism similar to logical short-circuiting or perhaps multithreaded partitioning (where intermediate variable are "not really created") takes place.

        发出的警告的标识符为MATLAB:colon:nonIntegerIndex.

        The issued warning's identifier is MATLAB:colon:nonIntegerIndex.

        • 也许存在下标参考的重载版本,其中存在检测下标本身是否为整数的初始步骤.如果不是,MATLAB会将其重定向"到其他一些类家族(示例).
        • This is what Steve Eddins of TMW had to say on the subject:

        ...在最初的日子里,MATLAB实现者倾向于在输入验证方面尽可能放宽.随着时间的流逝,我们意识到这种理念并不一定总是适合用户的,因此我们开始使某些语言规则变得更加严格和规范.一个示例是引入有关无效索引(非整数,非正数等)的错误消息.但是,我们无法始终按照自己喜欢的方式收紧行为.有时是因为我们发现太多的用户代码正在利用原始行为.这就是您在某些地方继续看到这种行为变化的原因之一. ...我建议用户仅使用整数索引.用户可以显式调用round或floor或任何方法,以将冒号运算符的输出转换为整数值.

      • 这篇关于Matlab是否接受非整数索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆