【问题标题】:Iterating over a vector of functions in MATLAB在 MATLAB 中迭代函数向量
【发布时间】:2011-08-05 21:10:23
【问题描述】:

是否可以遍历 MATLAB 中的函数列表?我正在尝试测试不同的径向基函数,这似乎是最好的方法。

【问题讨论】:

    标签: function matlab iteration


    【解决方案1】:

    您可以创建 cell arrayfunction handles 并对其进行迭代。例如:

    vec = 1:5;                            % A sample vector of values
    fcnList = {@max, @min, @mean};        % Functions to apply to the vector
    nFcns = numel(fcnList);               % Number of functions to evaluate
    result = zeros(1, nFcns);             % Variable to store the results
    for iFcn = 1:nFcns
      result(iFcn) = fcnList{iFcn}(vec);  % Get the handle and evaluate it
    end
    

    【讨论】:

      【解决方案2】:

      如果你想定义自己的函数,事实证明你可以这样做,按照 gnovice 的回答:

      funcList = {@(x, y) (x - y), @(x, y) (x + y)}
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-28
      • 1970-01-01
      • 2016-02-18
      相关资源
      最近更新 更多