Colon input to anonymous function
Show older comments
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.
Accepted Answer
More Answers (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
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!