>> p=[1;2;3]
p =
1
2
3
>> p1 = [2;3;4]
p1 =
2
3
4
>> p + p1
ans =
3
5
7
Run Code Online (Sandbox Code Playgroud)
但
>> p .+ p1
Error: "p" was previously used as a variable, conflicting with
its use here as the name of a function or command.
See "How MATLAB Recognizes Command Syntax" in the MATLAB
documentation for details.
Run Code Online (Sandbox Code Playgroud)
也
>> p .* p1
ans =
2
6
12
>> p * p1
Error using *
Inner matrix dimensions must agree.
Run Code Online (Sandbox Code Playgroud)
Bas*_*els 15
问题是运营商.+不存在:
>> help ops
Operators and special characters.
Arithmetic operators.
plus - Plus +
uplus - Unary plus +
minus - Minus -
uminus - Unary minus -
mtimes - Matrix multiply *
times - Array multiply .*
mpower - Matrix power ^
power - Array power .^
...
Run Code Online (Sandbox Code Playgroud)
注意,对于乘法,有两个运算符:.*,它是逐元素乘法,并且*是矩阵乘法.没有矩阵加法这样的东西,所以只有一个运算符+,它是元素加法.
当您键入时p .+ p1,Matlab解析器无法识别有效的运算符,因此它可能假设您正在使用命令语法并尝试使用字符串文字进行函数调用p('.+', 'p1').由于p不是函数,因此您会看到错误消息.
这种"命令语法"很方便,因为它可以节省你输入几个字符(即load data.mat代替load('data.mat').)问题是,这会导致解释语句时出现歧义,请参阅由错误消息直接链接的页面.这可能会让人感到惊讶结果,正如你的问题所示.这是Matlab语法的一个阴暗面.