将功能句柄的Cell数组转换为功能句柄的单个数组 [英] 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.
推荐答案
如果A
是包含函数的单元格数组,则使用func2str
获取字符串中的函数定义,并使用str2func
获取所需的函数.处理:
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.
这篇关于将功能句柄的Cell数组转换为功能句柄的单个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!