矩阵或向量参数的冒号运算符(:)的行为 [英] behavior of colon operator (:) with matrix or vector arguments
问题描述
我们都知道matlab冒号运算符可以创建线性序列,即
We all know the matlab colon operator to create a linear sequence, i.e.
1:5 = [1 2 3 4 5]
现在,我发现冒号运算符的参数也可以应用于向量或矩阵.但是我不理解后面的定义.
Now I found that the arguments of the colon operator can also be applied to vectors or matrices. However I do not understand the definition behind.
示例
[1 2 3 4]:5 == [1 2 3 4 5]
[1 2; 3 4]:3 == [1 2 3]
这是为什么?
第二个参数也可以是向量或矩阵.
The second argument can be vector or matrix as well.
最终我想了解
1:2:3:4:5
在matlab和[1 5]
中完全合法!
which is fully legal in matlab and [1 5]
by the way!
注意1:2:3:4:5:6
保持关联,即被解析为((1:2:3):4:5):6
.
Note 1:2:3:4:5:6
is left associative i.e. parsed as ((1:2:3):4:5):6
.
那么带有矩阵/矢量参数的冒号运算符的行为是什么?
So what is the behavior for the colon operator with matrix/vector arguments?
更正了左联想性的陈述.
corrected the statement of left associativity.
推荐答案
文档对于冒号运算符说:
如果指定非标量数组,则MATLAB会将j:i:k解释为j(1):i(1):k(1).
If you specify nonscalar arrays, MATLAB interprets j:i:k as j(1):i(1):k(1).
您的第一个示例被解释为1:3
,第二个示例被解释为1:5
Your first example is interpreted as 1:3
, the second as 1:5
具有两个以上:
的表达式被解析为左关联:
Expressions with more than two :
are parsed left-associative:
a:b:c:d:e==(a:b:c):d:e
.
>> 1:2:3:4:5
ans =
1 5
这篇关于矩阵或向量参数的冒号运算符(:)的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!