Colon input to anonymous function
显示 更早的评论
If I define a function:
F = @(x,y) (x^2-y)
If I call the function with two scalar inputs it works as expected,
F(2,3) = 1,
Calling with a single colon input returns an "index exceeds matrix dimensions" error, and calling with two colon inputs
F(:,:) = 3306
evaluates the function as if each input was the seemingly arbitrary scalar 58.
Can someone explain this behavior or link me to some documentation explaining this behavior? There doesn't seem to be anything about colon inputs on the anonymous functions doc page
Thanks.
采纳的回答
更多回答(1 个)
Colons are treated as char variables throughout MATLAB and the ASCII code for ':' is 58. In general, it is possible to do arithmetic and other numerical operations with character variables, in which case MATLAB assumes you want to do the arithmetic with the underlying ASCII values, e.g.,
>> ':'+':'
ans =
116
Want to know what you get when you cross a dog with a cat?
>> cross('dog','cat')
ans =
2885 -1403 -1289
类别
在 帮助中心 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!