Matlab inline VS anonymous functions

Is there any good reason to choose between using built-in functions and anonymous functions in MATLAB? This exact question was asked and answered here , but the answer does not help users of MATLAB recruits, because the code fragments are incomplete, therefore they do not start when pasted into the MATLAB command window. Can someone give an answer with code snippets that can be inserted into MATLAB?

+3
source share
2 answers

Anonymous functions replaced the built-in functions (as indicated in both documents and in the link you posted)

Documents warn:

inline will be removed in the next version. Use anonymous functions instead.

+8

:

1 - a xin

a = 1;
y = @(x) x.^a;
xin = 5;
y(xin) 
% ans =
%      5

2 - a , , a

a = 3;
y(xin)
% ans =
%      5

3 - , , undefined

clear all
y = @(x) x.^a;
xin = 5;
y(xin)
% ??? Undefined function or variable 'a'.

% Error in ==> @(x)x.^a

z = inline('x.^a','x');
z(xin)
% ??? Error using ==> inlineeval at 15
% Error in inline expression ==> x.^a
% ??? Error using ==> eval
% Undefined function or variable 'a'.
% 
% Error in ==> inline.subsref at 27
%     INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);

4 a .

clear all;
y = @(x,a) x.^a;
ain = 2;
xin = 5;
tic, y(xin, ain), toc
% ans =
%     25
% Elapsed time is 0.000089 seconds.

tic, z = inline('x.^a','x','a'), toc
z(xin, ain)
% z =
%      Inline function:
%      z(x,a) = x.^a
% Elapsed time is 0.007697 seconds.
% ans =
%     25

, → .

+2

Source: https://habr.com/ru/post/1690499/


All Articles