matlab - 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
解决方案
推荐阅读
- activerecord - ActiveRecord / Psql - 如何在数组中查找列和项目的记录
- javascript - 如何在输入中获取 div 的背景颜色 - JavaScript
- python - 对于 linux 用户,是否有一个简单的替换 `from win32api import keybd_event, GetAsyncKeyState`?
- vim - vim - 打开 utf16le 文件不能正确显示字符
- java - 如何在回收站视图中一对一滚动?
- php - Laravel 生成的 SQL 不起作用,但如果我直接在 PhpMyAdmin 上运行 SQL,它就可以工作
- recursion - jena shacl 形状最大递归
- python - 在多处理过程中加载 PyTorch 模型会闪烁 cmd
- reactjs - 在 React Final Form 中使用 FormSpy 替代 Redux-Form isDirty 的正确方法是什么?
- android - Android构建警告将新ns映射到旧ns