필터 지우기
필터 지우기

Which one is correct ? Using (' ') or @(var)

조회 수: 2 (최근 30일)
Aji Bowo
Aji Bowo 2021년 11월 7일
댓글: Aji Bowo 2021년 11월 17일
I found that using (' ') can result in the same value on feval(), or other functions like fplot(), instead of using @(x). What is the difference? Also I read documentation and inline(expr) works too, even though it's rather not recommended. For example :
feval('sin',[pi/2,pi])
%or
fplot('2*x^3+2.*cos(2*x).*sin(2*x)+3.*tan(2*x)',[-5 5], '--r')
%or example for @()
feval(@(x) sin(x),[pi/2,pi])
fplot(@(x) 2*x^3+2.*cos(2*x).*sin(2*x)+3.*tan(2*x),[-5 5], '--r')
  댓글 수: 2
Image Analyst
Image Analyst 2021년 11월 8일
편집: Image Analyst 2021년 11월 8일
I don't understand the question. Where are you using either '' or @????
Exactly what is not recommended? Reading the documentation . . . or some particular function like feval() or fplot()?
Aji Bowo
Aji Bowo 2021년 11월 8일
I use ' ' at 'sin' and for @ doesnt we usually use @ for anonymous function? and sorry for that typo i mean i read that code inline() works too but its not recomended it said in documentation

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2021년 11월 8일
편집: Walter Roberson 2021년 11월 8일
If you pass in a character vector to fplot, then fplot will
  1. test whether the input has no names in it (such as if it is a quoted numeric constant). In this case, it will convert the number into an anonymous function that returns a copy of the constant the same size as the input;
  2. test whether the vector is the name of a built-in function, or there is an executable file by that name, and if so will build a function handle from the name.
  3. Otherwise, it will vectorize() the character vector and build an anonymous function using the default variable ordering returned by symvar() for the parameter names
In all three cases the result is a function handle; a "simple" handle for the second case and an anonymous function for the other two cases.
The difference between that and passing in an anonymous or function handle you built yourself, is that if you built your own, then potentially you did so only once and re-use the handle, whereas fplot() needs to build the function handle each time it is executed. Also, if you pass in a character vector that is the name of an executable function, it must be the name of a "top-level" function -- one that has its own file, not a nested or local function.
The work done by fplot() is a convenience compared to building your own handle.... unless you are referring to a local function or nested function, in which case you need to build your own handle.
  댓글 수: 2
Walter Roberson
Walter Roberson 2021년 11월 8일
편집: Walter Roberson 2021년 11월 8일
A number of routines such as ode45() that normally expect function handles, internally use feval() to evaluate. Because of that, they can accept character vectors that are the names of built-in functions, or executable functions. However, if you pass in a character vector that is the name of an executable function, it must be the name of a "top-level" function -- one that has its own file, not a nested or local function; if you a referring to a local function or nested function, you need to build your own handle.
It is not specified exactly how feval() works, other than that it "follows the same scoping and precedence rules as calling a function handle directly"
Walter Roberson
Walter Roberson 2021년 11월 8일
There is one case where passing in a character vector has historically been better than a function handle:
Historically, arrayfun() had a set of built-in character vectors that it could detect and which it could process more efficiently than a function handle. I do not recall now which names they were, other than I recall that 'length' was one of them. arrayfun() was historically implemented as a .m file that you could read the source for. These days, arrayfun() has its functionality built-in, and I do not know if anything is special-cased anymore.
The local expert on getting speedups through that method is @Jan -- perhaps he can tell us whether this is still the case.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2021년 11월 8일
Some older functions allow you to specify the name of a function to call. But if you're writing code to use in a recent release of MATLAB, prefer using an anonymous function or a regular function handle.
Don't use inline objects either. Those are very old and have some idiosyncracies that function handles or anonymous functions don't. For instance, composing anonymous functions is easy. Composing inline objects is most certainly not.
f = @(x) 2*x;
g = @(x) x.^2;
h = @(x) f(g(x)); % 2*x.^2
h(5) % 2*5.^2 = 50
ans = 50
f2 = inline('2.*x', 'x');
g2 = inline('x.^2', 'x');
h2 = inline('f(g(x))', 'f', 'g', 'x')
h2 = Inline function: h2(f,g,x) = f(g(x))
h2(f2, g2, 5) % Yes, you have to pass f2 and g2 into h2
ans = 50
  댓글 수: 1
Aji Bowo
Aji Bowo 2021년 11월 17일
Thank you for the answer :D

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by