Wrong number of arguments to operator error

11 Ansichten (letzte 30 Tage)
Scott Gronholm
Scott Gronholm am 19 Jan. 2018
Bearbeitet: John D'Errico am 19 Jan. 2018
What does a “wrong number of arguments to operator” error indicate?
  6 Kommentare
Stephen23
Stephen23 am 19 Jan. 2018
Bearbeitet: Stephen23 am 19 Jan. 2018
@Scott Gronholm: what are i, j, F2, F3, and F1 exactly?
Your code might be missing a multiplication: MATLAB does not implicitly multiply two things written next to each other, which is a common shorthand people use when writing expressions. So if i is a variable then (F2+F3)i is an error, and you need (F2+F3)*i, or perhaps using element-wise multiply .*. But without knowing what those variables are this is just guessing.
Steven Lord
Steven Lord am 19 Jan. 2018
Stephen: there's at least one sort-of exception to your statement "MATLAB does not implicitly multiply two things written next to each other" and it is relevant here.
x = 2+3i
Even if there is a variable named i in the workspace, this will unambiguously represent the complex number with real part 2 and imaginary part 3. That syntax works only when what precedes the i or j is a number. It does not support variable names or expressions. These all work:
x = 2+3i
y = -4-5.3j
z = -1i
The existence of that syntax might have misled Scott into thinking his code, like example a below, would work. It doesn't. Other similar examples that will not work are below. I've listed what you should write as a comment after each incorrect command.
a = 2+(3+4)i % 2+(3+4)*1i
b = 7-pij % 7-pi*1j
c = pi+Infi % pi+inf*1i
d = 2+i3 % 2+3i
Of course b, c, and d won't error if you have variables (or functions that have a 0-input syntax) named pij, Infi, or i3 accessible to MATLAB. But they may not do what you expect. Note that I used 1i or 1j in the replacement expressions to avoid needing to worry if there is a variable or function named i so I could even use those inside a for loop whose loop variable is i.
for i = 1:10
c(i) = i+2i;
end
c2 = (1:10)+2i;
isequal(c, c2) % true
Of course, defining c that way is risky. It would be easy for someone to skim the code and replace "i+2i" with "3i" or "3*i". I said I could do that, not that I should.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

John D'Errico
John D'Errico am 19 Jan. 2018
Bearbeitet: John D'Errico am 19 Jan. 2018
Most operators are either monadic (one operand) or dyadic (two operands). This refers to the number of arguments the operator works on. I cannot think of any zero-adic operators off the top of my head. But I suppose you could argue i or pi to be that.
Thus transpose is a monadic operator. We can use it in the form
V'
So ' forms the transpose of V. V is the operand, and transpose works on only ONE operand.
Likewise, the unary minus is a monadic operator. Thus
-a
takes a and returns the negative of a. Note that minus has both a monadic and dyadic form, as does plus.
A dyadic operator is like the multiplication operator (* or .*). So, we can write
a.*b
Thus times takes two operands. It is dyadic.
However, we cannot write the operation
a'b
transpose is not defined as a dyadic operator. If I try to do so, I will see an error.
2'3
2'3
Error: Unexpected MATLAB expression.
Likewise, this generates an error.
*3
*3
Error: Unexpected MATLAB operator.
To be honest, I do not recognize the error message you have reported as occurring in any context that I can think of. So it may well be possible that some toolbox I do not have does this, even a TB written by a vendor or found on the FEX. But you have not told us what you are doing or what generated the indicated error message.

John D'Errico
John D'Errico am 19 Jan. 2018
Bearbeitet: John D'Errico am 19 Jan. 2018
Answer, based on the comment by Scott:
The thing is, it appears that i is not intended here as sqrt(-1), nor is j.
I would presume from the comment, that i is intended as a vector, typically the unit vector [1 0 0]. Likewise, j is PROBABLY intended as the unit vector [0 1 0]. These are fairly standard notations. So in that context, we would want to write
F=(F2+F3)i + F1 j
as
F = (F2+F3)*[1 0 0] + F1*[0 1 0];
Of course, you could just recognize that this reduces to the vector:
F = [(F2+F3), F1, 0];
In a strictly 2 dimensional universe, we might have
F = (F2+F3)*[1 0] + F1*[0 1];
Again, this is only conjecture. No matter what, you need to use * there to tell MATLAB to use multiplication.

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by