Does Matlab accept non-integer indices?

后端 未结 1 1868
萌比男神i
萌比男神i 2021-01-31 17:04

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

1条回答
  •  一生所求
    2021-01-31 17:12

    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.

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

    Theories:

    • Perhaps there exist overloaded versions of the subscripted reference where there is an initial step of detecting whether the subscripts themselves are integers or not. In case they're not, MATLAB "redirects" this to some other family of classes (example).

    Official comments:

    • This is what Steve Eddins of TMW had to say on the subject:

      ... in its earliest days, MATLAB implementers had a tendency to be as permissive as possible with respect to input validation. Over time, we realized that this philosophy wasn’t always the best one for users, and we started making some of the language rules tighter and more regular. One example was the introduction of error messages about invalid indices (noninteger, nonpositive, etc.). However, we couldn’t always tighten up the behavior as much as we liked. Sometimes that was because we discovered that too much user code was exploiting the original behavior. That is one of the reasons you continue to see this kind of behavior variation in some places. ... I would advise users to only use integer-valued indices. Users can explicitly call round or floor or whatever to convert the output of the colon operator to be integer-valued.

    0 讨论(0)
提交回复
热议问题