Info
此问题已关闭。 请重新打开它进行编辑或回答。
Need help with array of functions
2 次查看(过去 30 天)
显示 更早的评论
Hello all, I have a script that uses symbolic variables heavily. Here is the code,
syms x y z
as = 3*x;
bs = 4*y + z;
cs = x*z;
ds = x+y+z;
F_g = [as;bs;cs;ds];
sel = [1 3 4 2 4 3 2];
theResult_sym = F_g(sel)
xv = 1;
yv = 2;
zv = 3;
subs(theResult_sym, {x y z}, {xv yv zv})
What it does is
- First, all the symbolic equations are saved in a vector F_g
- According to the value specified in sel, pick the corresponding equations in F_g to evaluate.
It has come to my notice that "eval" is quite computationally expensive. I'd like to first convert the symbolic equations to function handles first. Then I will perform calculations. Here is my code
syms x y z
as = 3*x;
bs = 4*y + z;
cs = x*z;
ds = x+y+z;
F_g = [as ;bs;cs;ds];
ParaSet_sym = [x,y,z];
F_g_fun = matlabFunction(F_g, 'vars', ParaSet_sym);
sel = [1 3 4 2 4 3 2];
Does F_g_fun store an array of function handles? If yes, I wonder how do I access its elements? I can't access with () because values in it would be considered as function parameters input.
1 个评论
Stephen23
2018-5-15
编辑:Stephen23
2018-5-15
"Does F_g_fun store an array of function handles?"
MATLAB does not support arrays of function handles: "A function handle is always scalar (1-by-1)."
It is possible to put any number of (scalar) function handles into a cell array, struct, or other container, but of course that is not the same thing as a function handle array.
Note that the matlabFunction describes its output as being a "Function handle that can serve as an input argument to numerical functions, returned as a MATLAB function handle." It clearly only mentions one function handle, and uses the singular everywhere.
回答(0 个)
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!