首页 > 解决方案 > MATLAB:替换多元函数以避免冗余计算?

问题描述

我有两个变量(数字输入和输出)的函数 F(x,y),它执行一些相当复杂的计算。我的程序有时会在任何单独的运行中调用这个函数数十万次。但是,它在每次运行期间使用相同的有限有序对 (x,y) 集。所以,我想在程序开始时对这些对计算 F 一次,然后存储此输入/输出信息以用于剩余的运行。

我最初是在考虑使用 Matlab 的 containers.Map 类。但是,这不允许使用 [x, y] 形式的键 - 每个键必须是单个数字/字符串。因此,我可以将所有输入/输出包含在一个 Nx3 数组中并构建我自己的查找函数。我想知道是否有任何可能更快的替代方法,或者在我没有看到的这种情况下使用容器的方法?

F = @(x,y) x + y;                      % Actual F is 100's of lines
inputs = [0 1; 0 2; 1 1; 1 2];         % Each row is (x,y) pair of only inputs I care about
z = F( inputs(:,1), inputs(:,2));      % Only outputs I care about
idealFunction = ?????                  % New function which only accepts (x,y) pairs above

idealFunction([0 2]);                  % Returns 2 by matching to element of z  
idealFunction([0 3]);                  % May throw an error but I don't care
                                       % since it's not in my inputs of interest

标签: matlabfunctionoptimization

解决方案


推荐阅读