如何从函数句柄中获取函数的一个子集,这是Matlab中函数的一个向量 [英] How to obtain a subset of functions from a function handle that is a vector of functions in Matlab
问题描述
F = @(x)[..
coeff1 * x(1)* x(4); ...
coeff2 * x(3); ...
coeff3 * x(7)* x(3)...
];
实际上它有150行。我需要提取行中不同子集的功能。例如,从 F
中的行 3:17
中创建一个新句柄。但我不能只是索引一个句柄。有没有解决方案?编辑:我需要该子集作为一个新的句柄,换句话说,我不能评估整个F,只选择解决方案行。
这个怎么样:
F = @(x)[
5 * x。^ 2 * x * 4;
6 * x;
12 * x。^ 2 * x * 3
];
newF = getFunHandles(F,2:3);
其中 getFunHandles
适用于任意范围,例如3:17
function f = getFunHandles(F,range)
funStr = textscan(func2str(F),' %S', '分隔符', ';');
funStr = regexprep(funStr {1},{'@(x)[',']'},{'',''});
newFunStr = strcat('@(x)[',strjoin(funStr(range),';'),']');
f = str2func(newFunStr);
In Matlab, I have a function handle defined as a vector like this
F = @(x) [...
coeff1*x(1)*x(4); ...
coeff2*x(3); ...
coeff3*x(7)*x(3) ...
];
In reality it has 150 rows. I need to extract different subsets the functions in the rows. For example make a new handle from the rows 3:17
of those in F
. But I can't just go indexing a handle. Is there a solution?
Edit: I need the subset as a new handle, in other words I can not evaluate the entire F and just select the solution rows.
Thanks in advance.
How about this:
F = @(x)[
5*x.^2*x*4;
6*x;
12*x.^2*x*3
];
newF = getFunHandles(F,2:3);
where getFunHandles
works for any arbitrary range, e.g. 3:17
function f = getFunHandles(F, range)
funStr = textscan(func2str(F),'%s','delimiter',';');
funStr = regexprep(funStr{1}, {'@(x)[', ']'}, {'', ''});
newFunStr = strcat('@(x)[',strjoin(funStr(range),';'),']');
f = str2func(newFunStr);
这篇关于如何从函数句柄中获取函数的一个子集,这是Matlab中函数的一个向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!