# How to turn a function handle with 3 inputs (1 variable and 2 parameters) and then assign the inputs parameters and get a function handle with one input? ?

조회 수: 3(최근 30일)
Mohammad Shojaei Arani 2022년 1월 14일
답변: Jeff Miller 2022년 1월 15일
Hello friends!
Consider the following
y=1;z=1;
f=@(x,y,z)x+y+z;
Now, I would like to create another function handle which is only a function of x. You migh suggest the following solution:
g=@(x)f(x,y,z);
but this is not what I want since I need to pass g(x) as input to another function and there I want to reduce the computational time. If I follow this solution whenever I call g, say g(1), it needs f, too plus that it does 3 arithmatics (3 sums) but I would like to have 2 arithmatics since g(x)=x+2.
Any idea?
##### 댓글 수: 7표시숨기기 이전 댓글 수: 6
Mohammad Shojaei Arani 2022년 1월 14일
NO. I did not say that. Of course, we need f in order to build g. But, the point in my question is how to do this in a cheap manner. If you look at my comminication with Rik, I mentioned that the following is a way to answer my question:
syms x y z
f=x+y+z;
g=subs(f,{y,z},{1,1});
g=matlabFunction(g);
But, this is computationally very expensive. In theory, Matlab has the capacity to solve my problem through function handles (not symbolic toolbox). But, it seems that matlab developers did not carefully think about this.

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

### 답변(3개)

Steven Lord 2022년 1월 14일
Let's look at the actual time difference between the two approaches.
y=1;
z=1;
f=@(x,y,z)x+y+z;
g=@(x)f(x,y,z);
h = @(x) x+2;
timeit(@() g(42))
ans = 2.8650e-06
timeit(@() h(42))
ans = 7.6423e-08
Is this really the bottleneck in your code?
I realize that this was probably just a simple example to demonstrate what you're trying to do, so can you show the output of timeit called on a more "realistic" f, g, and h functions from your actual application?
##### 댓글 수: 3표시숨기기 이전 댓글 수: 2
Matt J 2022년 1월 15일
Yes, what steps did you take that led you to such a monstrous 100MB mathematical expression? It sounds as if you have done nothing at all to vectorize the operations in your equations, like for example if you had rewritten a large matrix/vector operation in terms of their individual scalar components.
I suggest you show us just the first 20 lines of the expression you're trying to evaluate, so we can get a sense of the problem.

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

Matt J 2022년 1월 14일
편집: Matt J 2022년 1월 14일
But, this is computationally very expensive for my super big expressions.
What if you use the optimize flag?
g=matlabFunction(f,'File','outfile','Optimize',true)
##### 댓글 수: 0표시숨기기 이전 댓글 수: -1

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

Jeff Miller 2022년 1월 15일
Not sure I understand the situation, but this sounds to me like a case where an OO approach could be very helpful. Consider:
classdef myFunc < handle
properties
ComputedFromYandZ
end
methods
function obj = myFunc() % Constructor
obj.ComputedFromYandZ = 0;
end
function [] = SetNewYandZ(obj,Y,Z) % Compute whatever depends on Y and Z
obj.ComputedFromYandZ = Y + Z;
end
function Result = ComputeFromX(obj,X) % Compute whatever depends on X with Y and Z fixed
Result = abs( X + obj.ComputedFromYandZ ) ;
% I included abs() here to make a function for fminsearch to
% minimize.
end
end
end
You can then use a function handle to a function that depends only on X (after setting Y and Z), something like this:
myF = myFunc;
myF.SetNewYandZ(2,3);
fminsearch(@myF.ComputeFromX,20)
% ans -5
myF.SetNewYandZ(-20,-30);
fminsearch(@myF.ComputeFromX,20)
% ans 50

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

### Community Treasure Hunt

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

Start Hunting!