inline VS anonymous functions
659 views (last 30 days)
Show older comments
I don't see differences between... but - maybe @fun is more wide than inline-function
>> a
a =
1
>> x=1:10
x =
1 2 3 4 5 6 7 8 9 10
>> y=@(x) x.^a
y =
@(x)x.^a
>> y(x)
ans =
1 2 3 4 5 6 7 8 9 10
>> a=3
a =
3
>> y(x)
ans =
1 2 3 4 5 6 7 8 9 10
>> z=inline('x.^a','x')
z =
Inline function:
z(x) = x.^a
>> z(x)
??? Error using ==> inlineeval at 15 Error in inline expression ==> x.^a Undefined function or variable 'a'.
1 Comment
Varoujan
on 8 Oct 2015
Firstly, Matlab help files say that 'inline' function will be removed in future revisions. So, you better stick with the anonymous functions.
Second, your definition of inline function is wrong. When defined as inline, you have to provide the function all the inputs it needs. Change your code as follows:
% Define the inline functon 'z'
z = inline('x.^a','x', 'a');
% Call the function with explicit variables
y = z(1:10,2);
% Define variables and call the function with those
x = 1:10;
a = 2;
y = z(x,a);
Accepted Answer
Oleg Komarov
on 20 May 2011
Step 1 - define anonymous function in constant a and variable x:
a = 1;
y = @(x) x.^a;
Step 2 - change constant in the workspace, but the anonymous function remains unchanged since a was taken as a parameter in step 1
a = 3;
y(x)
Step 3 - in both cases the constant a is not defined at the moment the anonymous and inline fcns are created, thus the error in both cases
clear all
y = @(x) x.^a;
y(1)
z = inline('x.^a','x')
z(x)
Step 4 alternatives
y = @(x,a) x.^a;
y(1,2)
z = inline('x.^a','x','a')
z(1,2)
inline is an eval wrapper and is much slower than anonymous fcns.
6 Comments
More Answers (2)
John D'Errico
on 26 Dec 2020
Edited: John D'Errico
on 26 Dec 2020
I think what people are not drawing attention to here are the real differences between inline and anonymous functions/function handles.
- Speed.
- SPEED.
- The ability to pass functions around, while embedding data inside the function handle.
- Do you really want to use a tool that will disappear at some point in the future, even if you don't know for sure when that will happen? inlines will go away one day, so we are told. You want to write code that will last, not code you will need to fix in the future.
Yes, I said speed twice there. Inline functions are slower. Is there a good reason why you want to use slower code?
x = 1:1000;
f1 = inline('x.^2 +2*x + exp(x)');
f2 = @(x) x.^2 +2*x + exp(x);
timeit(@() f1(x))
timeit(@() f2(x))
So the inline function will be considerably slower.
Next, a function handle makes it easy to pass data around between function workspaces. This is something that people think they need to use global avariables for. NOT true. A function handle is a great way to pass data into an optimization, for example.
a = 2;
b = 3;
obj = @(x) sin(a + b*x);
[xmin,fval] = fminsearch(obj,rand(1))
The function handle in obj encapsulates the current values of a and b into the function. I can pass obj around to anything.
[xz,fval] = fzero(obj,rand(1))
Can you do the same thing using an inline function? That takes far more work.
I'd say a welcome good bye to inline functions whenever they do go away. They were useful in their day, but function handles are far better, far more valuable.
1 Comment
Stephen23
on 27 Dec 2020
Edited: Stephen23
on 27 Dec 2020
"Is there a good reason why you want to use slower code?"
My good reasons are:
- My professor taught themselves to code in the early 1960's and that is how they are teaching my class in 2020: there is no point in writing faster code, because we cannot load the punched cards any faster. Admittedly using punched cards is much more difficult these days since in the last few years many laptops no longer have that little slot/tray thing that we have to push them into.
- I am learning from the book "MATLAB 3.5 for the Intel 386". Over a quarter of a million transistors!
- Slower code is much less likely to crack the tiny memory toroids, which are not easy to rethread.
- The big bold banner (which has been there for nearly ten years) at the very top of the inline page states but it is not clear to me what this is trying to tell me. So I ignore it.
On an unrelated note, why does GOTO throw an error when I try to use it?
Jetze Sikkema
on 13 Dec 2016
To start with the theory: there is a significant conceptual difference between inline and anonymous functions:
- Inline tells the compiler to insert the function body directly into the code, which save the effort of calling the function at the cost of making the calling code bigger. This is a low level optimization technique that is independent of the programming paradigm.
- Anonymous functions are function 'objects'. They are stored in a variable and contain knowledge about the local variables at the time of creation (form a clusore). They work like a normal variable and can be overwritten, manipulated etc. They are an essential feature of functional programming and are not specifically intended as an optimalization.
That much for the theory in practice:
- In Matlab however they seem to do the same and inline seems scheduled for decomissioning so it should not be used: Only use the anonymous functions.
- Most compilers can nowadays figure out when to inline functions and the overhead of calling a function is almost always extremely low.
1 Comment
Steven Lord
on 13 Dec 2016
The introduction of inline objects was before my time and before the introduction of function handles in release R12 (MATLAB 6.0) (just slightly before the start of my tenure at MathWorks) and anonymous functions in release R14 (MATLAB 7.0). For context, release R12 was released in November 2000 and release R14 was released in June 2004 according to the table in Wikipedia.
I believe the reason those objects were called inline is because you could define simple functions in one line of your code without having to create a separate subfunction in your function file or a completely new function file.
My recommendation is that unless you need compatibility with a twelve and a half year old version of MATLAB, use anonymous functions.
See Also
Categories
Find more on Function Creation 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!