inline VS anonymous functions

459 Ansichten (letzte 30 Tage)
Igor am 20 Mai 2011
Bearbeitet: Stephen23 am 27 Dez. 2020
I don't see differences between... but - maybe @fun is more wide than inline-function
>> a
a =
>> x=1:10
x =
1 2 3 4 5 6 7 8 9 10
>> y=@(x) x.^a
y =
>> y(x)
ans =
1 2 3 4 5 6 7 8 9 10
>> a=3
a =
>> 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 Kommentar
Varoujan am 8 Okt. 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);

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Oleg Komarov
Oleg Komarov am 20 Mai 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;
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;
z = inline('x.^a','x')
Step 4 alternatives
y = @(x,a) x.^a;
z = inline('x.^a','x','a')
inline is an eval wrapper and is much slower than anonymous fcns.
  6 Kommentare
Igor am 20 Mai 2011
I asked another "why" -- concerning inline.
The error is due to "not enough input args" (like this), OR due to parser at calling ">> z(x)" hasn't recognized "a" as a fact variable.
Oleg Komarov
Oleg Komarov am 20 Mai 2011
I answered you in step 3

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

John D'Errico
John D'Errico am 26 Dez. 2020
Bearbeitet: John D'Errico am 26 Dez. 2020
I think what people are not drawing attention to here are the real differences between inline and anonymous functions/function handles.
  1. Speed.
  2. SPEED.
  3. The ability to pass functions around, while embedding data inside the function handle.
  4. 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))
ans = 2.0982e-04
timeit(@() f2(x))
ans = 1.1410e-05
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))
xmin = 0.9041
fval = -1.0000
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))
xz = 0.3805
fval = 1.2246e-16
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 Kommentar
Stephen23 am 27 Dez. 2020
Bearbeitet: Stephen23 am 27 Dez. 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?

Melden Sie sich an, um zu kommentieren.

Jetze Sikkema
Jetze Sikkema am 13 Dez. 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 Kommentar
Steven Lord
Steven Lord am 13 Dez. 2016
The inline object in MATLAB is not related to inline functions in languages like C and C++.
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.

Melden Sie sich an, um zu kommentieren.


Mehr zu Function Creation 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