anonymous function with a variable number of input variables
古いコメントを表示
Hi!
I have an anonymous function:
ht = @(A1,A2,A3,....) ...
meaning that I don't know in advance how many variables Ai are included in the definition of ht. Then I need to minimise ht in respect of the variables Ai. For example, for 2 indipendent variables I get a result with:
ht2 = @(A)ht(A(1),A(2));
A0 = [1,1];
[res,fval] = fminunc(ht2,A0)
How can I define ht2 dinamically with a variable number of unknown Ai?
Thanks!
採用された回答
その他の回答 (1 件)
per isakson
2020 年 5 月 10 日
編集済み: per isakson
2020 年 5 月 10 日
"anonymous function with a variable number of input variables"
Something like this?
>> ht(1,2,3)
ans =
6
>> ht(1,2,3,4)
ans =
10
>> ht(1,2,3,4,5)
ans =
15
>>
where
ht = @(varargin) dario( varargin{:} );
and
function out = dario( varargin )
out = sum([ varargin{:} ]);
end
3 件のコメント
dario
2020 年 5 月 10 日
per isakson
2020 年 5 月 10 日
I assume that ht shall returns a scalar numerical output. That output could readily be calculated in dario, since all inputs are available. The variables, Ai, what type and size are they?
dario
2020 年 5 月 10 日
カテゴリ
ヘルプ センター および File Exchange で Surrogate Optimization についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!