필터 지우기
필터 지우기

Intermediate calculation in function handle

조회 수: 3 (최근 30일)
StarBucK
StarBucK 2018년 10월 31일
댓글: StarBucK 2018년 11월 1일
Hi
I would like to know if it is possible to make intermediate calculation in a function handle.
For example, making something like this functional :
@(x) y=1+1; x+y;
This function handle would return me 3 for example for x=1. y is thus an intermediate variable of a calculation inside the function handle.
Is there a way to do it ?
Thanks

채택된 답변

the cyclist
the cyclist 2018년 10월 31일
I don't really understand what you want to do. I'm guessing you deliberately chose the simplest possible example, and that what you really want to do is more complicated. But since your example is trivially
@(x) x + 2
I can't quite get what you really need.
Maybe you mean something like this?
y = @(z) z + 1; % The intermediate function?
f = @(x,g) x + g; % The final function
f(1,y(1)) % Calling syntax
  댓글 수: 1
StarBucK
StarBucK 2018년 11월 1일
Indeed my problem was more general but i found a solution by writing it differently. But at least now I know that anonymous functions can indeed just have a single expression inside and we have to do tricks to make intermediate calculations. Thanks

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

추가 답변 (0개)

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by