MATLAB的赋值执行顺序

uelo1irk  于 8个月前  发布在  Matlab
关注(0)|答案(1)|浏览(103)

为什么

x = 1:7;
x(1:8) = 1:9;

Unable to perform assignment because the left and right sides
have a different number of elements.

而不是

Index exceeds the number of array elements. Index must not
exceed 7.

在Python中是这样的:首先,计算=的右边,然后计算=的左边,然后计算=

import numpy as np
x = np.arange(7)
x[7] = np.arange(8)[8]  # next try `[8]` -> `[7]`

(当不索引单个元素时,修改示例为0:7 == 0:6 == 0:999

h6my8fg2

h6my8fg21#

我不认为您可以从错误消息中推导出“执行顺序”。在你的例子中,Python只是碰巧在抛出一个错误之前抛出另一个错误。
然而,在MATLAB中,我们有函数subsasgn,它是为语法x(s) = y的自定义类调用的。这表明左边的索引和赋值是一个单独的操作,你不能只做一个而不做另一个。左边的索引不会触发对索引数组的求值,我们不读取当前在那里的内容,我们只是写入这些位置。

相关问题