将函数句柄的元胞数组转换为单个函数句柄数组 [英] Converting Cell array of function handle into a single array of function handle
问题描述
我需要建立一个非线性方程的向量,以便在 fsolve
中使用来求解它.但是我应该在每次循环迭代中制作向量的每个元素.我怎样才能组成这样一个向量?事实上,我不能使用元胞数组.如何转换像 {@(x) x(1)+x(2)^2; 这样的元胞数组;@(x) x(1)-2*(x(2))}
成一个数组,如 @(x) [ x(1)+x(2)^2 ;x(1)-2*(x(2))]
?因为我想用fsolve
来求解非线性方程组.
I need to build up a vector of non-linear equations to be used in fsolve
to solve it. But I should make each element of the vector in each loop iteration. How can I make up such a vector? In fact, I can not use cell array. How can I convert a cell array like {@(x) x(1)+x(2)^2; @(x) x(1)-2*(x(2))}
into an array like @(x) [ x(1)+x(2)^2 ; x(1)-2*(x(2))]
? Because I want to use fsolve
to solve the system of non-linear equations.
推荐答案
使用 func2str
获取字符串中的函数定义并使用 str2func
获取所需的函数,如果A
是包含函数句柄的元胞数组:
Use func2str
to get the function definitions in string and use str2func
to get the desired function, if A
is the cell array containing the function handles:
B = strcat(regexprep(cellfun(@func2str, A, 'uni', 0), '^@\(x\)', ''), ';');
F = str2func(strcat('@(x) [', B{:}, ']'));
现在 F
包含所需的函数句柄.
Now F
contains the desired function handle.
这篇关于将函数句柄的元胞数组转换为单个函数句柄数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!