Why. + Arithmetic operations fail on a vector of the same size

>> p=[1;2;3] p = 1 2 3 >> p1 = [2;3;4] p1 = 2 3 4 >> p + p1 ans = 3 5 7 

But

 >> 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. 

Also

 >> p .* p1 ans = 2 6 12 >> p * p1 Error using * Inner matrix dimensions must agree. 
+7
matlab
source share
2 answers

The problem is that the .+ Operator does not exist:

 >> 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 .^ ... 

Note that for multiplications there are two operators:. .* , Which is multiplication by elements, and * , that is multiplication of the matrix . There is no such thing as matrix addition, therefore there is only one + operator, which is an elementary complement .

When p .+ p1 entered, the Matlab parser does not recognize the actual operator, so it is likely that you are using the command syntax and trying to call a function call with string literals p('.+', 'p1') . Since p not a function, you get the error message that you see.

This "command syntax" is convenient in that it can save you a few characters (i.e. load data.mat instead of load('data.mat') ). The problem is that this leads to ambiguity in the interpretation of the statements, see the page that was directly related to your error message. This may produce unexpected results, as your question shows. This is one of the shady sides of the Matlab syntax.

+14
source share

The ".*" Operator multiplies elements by two arrays.

The "*" operator performs multimetric multiplication of two arrays, which in your case cannot be performed on two 3x1 vectors, so Matlab reports an error.

The ".+" Operator does not exist in Matlab. In this case, Matlab believes that you use the syntax "." to refer to an element of a structure or function, therefore, to the error you receive.

+3
source share

All Articles