Reputation: 9146
Let's say I want to store the function y(x) = x + 2
in a variable.
Is there any way to save y = x + 2
, and access as y(x)
? For example, y(2)
?
Upvotes: 2
Views: 615
Reputation: 125854
An anonymous function is what you're looking for:
>> y = @(x) x+2;
>> y(2)
ans =
4
Upvotes: 4