How can I re-write an anonymous function into a standard function?
3 ビュー (過去 30 日間)
古いコメントを表示
Jesús Isaac Vázquez Serrano
2019 年 11 月 8 日
編集済み: David Goodmanson
2019 年 11 月 10 日
I'm new to matlab. I would like to rewrite the following anonymous function into a standard function but I got struggled with the syntax.
Original function:
objc = @(w) [-mu'*w' w*C*w'];
My adjustment:
function [returns,variance] = MultiObjective(mu,C)
returns = -1*mu'*w';
variance = w*C*w';
end
1 件のコメント
dpb
2019 年 11 月 9 日
The anonymous function has only w as the parameter; mu, C are embedded into the function definition with the values they contain at the time the function is defined.
Your function, would also have to pass w
採用された回答
David Goodmanson
2019 年 11 月 9 日
編集済み: David Goodmanson
2019 年 11 月 10 日
Hi Jesus,
[ CORRECTED to include a minus sign ]
function [returns,variance] = MultiObjective(mu,C,w)
returns = -mu'*w';
variance = w*C*w';
end
Since it is a normal (non-anonymous) function, all variables are passed in on input, in whatever order you choose. (Of course you could define hardwired constants inside the function as well). Also, no need to multiply by -1.
1 件のコメント
Walter Roberson
2019 年 11 月 9 日
Note that returning two variables is not the same as returning a row vector of two values.
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!