MATLAB Answers

Igor
1

inline VS anonymous functions

Asked by Igor
on 20 May 2011
Latest activity Commented on by Steven Lord
on 13 Dec 2016

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

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);

Log in to comment.

Products

No products are associated with this question.

2 Answers

Answer by Oleg Komarov on 20 May 2011
 Accepted Answer

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

Hi Igor,

for the "why": anonymous functions were "invented" as a replacement for the (somewhat ugly) inline.
Anonymous functions are much more robust then inline, same holds for using function handles in general instead of strings. Your example shows the difference: the function

@(x) x.^a

captures a at this very moment. What ever happens to a does not make a difference. This is in line with general behaviour: if you write

a = 42
x = 2*a;
a = 1;

you won't expect x to be 2. If a is indeed to be variable, use step 4.

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.

I answered you in step 3

Log in to comment.


Answer by 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

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.

Log in to comment.


Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!