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!