フィルターのクリア

Passing functions with fixed input

3 ビュー (過去 30 日間)
Matteo Tesori
Matteo Tesori 2023 年 12 月 29 日
編集済み: Stephen23 2023 年 12 月 29 日
Consider the following function
function f = fun1(x, a)
f = a * x;
end
here x is a scalar variable and a is a scalar parameter. Now consider this second function
function g = fun2(fun)
x = rand;
g = f(x, 1);
end
if we pass @fun1 to fun2, then fun2 evaluates fun1 over the variable x with the parameter a fixed to the value 1.
I'm wondering if it is possible to pass to fun2 the function fun1 with a given parameter a fixed, e.g. a=1.
In my main script I would like to write something like
g = fun2(@fun1(x, 1));
where I'm intending that the first input argument x will be defined inside fun2, while the parameter a is already fixed to the value 1, so that is not necessary to define a inside function fun2

採用された回答

Stephen23
Stephen23 2023 年 12 月 29 日
編集済み: Stephen23 2023 年 12 月 29 日
The MATLAB documentation covers this here:
See also:
g = fun2(@(x)fun1(x,1))
g = 0.3941
function f = fun1(x, a)
f = a * x;
end
function g = fun2(fnh)
x = rand;
g = fnh(x);
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by