[英]Apply Function Handle to Multiple Input Arguments
I have a matrix and a vector in MATLAB defined: 我在MATLAB中定义了一个矩阵和一个向量:
A=rand(3);
x=rand(3,1);
And a function that takes these types of input arguments: 还有一个采用以下类型的输入参数的函数:
b = MacVecProd(A,x);
However, I'd like to use this function's function handle in order to apply it to my values. 但是,我想使用此函数的函数句柄以将其应用于我的值。 I thought that I could use cellfun
for this, but: 我以为可以为此使用cellfun
,但是:
v = {A,x};
cellfun(@MatVecProd_a, v{:})
Gives the error: 给出错误:
Error using cellfun
Input #2 expected to be a cell array, was double instead.
How do I do this correctly? 如何正确执行此操作?
You could define your own, special function to call anonymous functions with given parameters, eg: 您可以定义自己的特殊函数来调用具有给定参数的匿名函数,例如:
% define special function to call function handles
myfuncall = @(fh, v) fh(v{:});
% execute MacVecProd using myfuncall
b = myfuncall(@MacVecProd, v)
Based on your comment that you have array of functions and you want to execute them for your input arguments, you could do as follows: 根据您的评论,您拥有函数数组,并且想要对输入参数执行它们,您可以执行以下操作:
% cell array of function handles
myFunctioins = {@MacVecProd, @MacVecProd2, @MacVecProd3};
% execute each function with v parameters
% I assume you want to execute them for the same input v
resultCell = cellfun(@(fh) fh(v{:}), myFunctioins, 'UniformOutput', 0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.