将功能句柄的Cell数组转换为功能句柄的单个数组 [英] Converting Cell array of function handle into a single array of function handle

查看:97
本文介绍了将功能句柄的Cell数组转换为功能句柄的单个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要建立一个非线性方程式的向量,以便在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆