How to get an specific element from an output vector of a function handle?

31 ビュー (過去 30 日間)
Guido Carvalho
Guido Carvalho 2021 年 11 月 18 日
コメント済み: Guido Carvalho 2021 年 11 月 18 日
Hello, I'm creating a function handle that outputs three values in the form of a vector, for example:
f = @(x) [x+1; x^2; 2*x];
so, in this case, f(1) gives me the vector [2; 1; 2].
How do I call, let's say, the second element of this vector, without defining the answer as another variable? Something like f(1)(2), if you know what I mean.

採用された回答

Stephen23
Stephen23 2021 年 11 月 18 日
You could define a simple wrapper function, e.g.:
f = @(x) [x+1; x^2; 2*x];
g = @(a,x)a(x);
g(f(1),2)
ans = 1

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by