简体   繁体   English

将函数句柄应用于多个输入参数

[英]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.

相关问题 如何将函数应用于MatLab中具有多个参数的数组? - How to apply a function to an array in MatLab with multiple arguments? Matlab:从函数外部检测函数句柄的输入自变量数量 - Matlab: Detect number of input arguments for function handle from outside of the function 类型为“ function_handle”的输入参数的未定义函数“ NR”。 MATLAB - Undefined function 'NR' for input arguments of type 'function_handle'. MATLAB 使用单个矩阵作为函数的多个输入参数 - Using a single matrix as multiple input arguments to a function 使用不同的参数集多次应用相同的函数 - Apply the same function multiple time with different sets of arguments function 句柄到 function 与多个 output 和文件数据存储中的输入变量 - function handle to function with multiple output and input variables in filedatastore 在 Matlab 中使用 fzero 时出错:未定义 function 或输入 arguments 类型为“function_handle”的方法“det” - Error using fzero in Matlab: Undefined function or method 'det' for input arguments of type 'function_handle' 如何使用输入 arguments 运行多个 matlab function 并具有相似的名称 - How to run multiple matlab function with input arguments and have similar names 功能中没有足够的输入参数 - Not enough input arguments in function 输入解析器和函数句柄 - input parser and function handle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM