Filter löschen
Filter löschen

Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

Need help with array of functions

1 Ansicht (letzte 30 Tage)
Stephen
Stephen am 15 Mai 2018
Geschlossen: MATLAB Answer Bot am 20 Aug. 2021
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 Kommentar
Stephen23
Stephen23 am 15 Mai 2018
Bearbeitet: Stephen23 am 15 Mai 2018
"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.

Antworten (0)

Diese Frage ist geschlossen.

Community Treasure Hunt

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

Start Hunting!

Translated by