- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写一个数学模型,它涉及在值网格上计算特定数量数千次,并使用一些变化的模型参数。目前,这太慢了,我正在寻求有关矢量化模型最密集部分的建议。
为了便于阅读,我目前已经有了它的基本实现,但现在希望如果可能的话对下面的整个代码段进行矢量化。代码段的最小示例是:
% Setup grid to evaluate and results vector
T_max = 10000;
eval_points = linspace(0, T_max, 1000);
results = zeros(size(eval_points));
% Function that is used in computation
Z_func = @(x, omega) (1./(omega.*sqrt(2*pi))).*exp( -(x.^2)./(2.*omega.*omega) );
% Random data for now, known in full problem
historic_weights = rand(1,100);
historic_times = rand(1,100);
% Fixed single parameter omega
omega = 0.5;
% Time evaluation
tic()
for eval_counter = 1:size(eval_points,2)
for historic_counter = 1:size(historic_weights,2)
temp_result = 0;
for k = 0:1:T_max
temp_result = temp_result + Z_func( eval_points(eval_counter) - historic_times(historic_counter) + 1440*floor(historic_times(historic_counter)/1440) - 1440*k, omega );
end % End of looping over k
results(eval_counter) = results(eval_counter) + historic_weights(historic_counter)*temp_result;
end % End of looping over weights
end % End of looping over evaluation points
toc()
在我的计算机上,评估只花了 60 秒多一点。我不想使用并行工具箱,因为我已经在其他地方使用它,并且在每个进程上都会调用显示的代码段。
如果这在 Matlab 中不可能,我很乐意在 Python 中尝试。
最佳答案
通过将 temp_result
和 result
作为矩阵而不是一次计算一个,您可以相当轻松地对内部两个循环进行矢量化。例如:
for eval_counter = 1:size(eval_points,2)
temp_result = sum(Z_func( eval_points(eval_counter) - historic_times + 1440*floor(historic_times/1440) - 1440*(0:1:T_max)', omega ));
results(eval_counter) = results(eval_counter) + sum(historic_weights.*temp_result);
end % End of looping over evaluation points
这在我的机器上运行大约需要 9 秒,而您的循环版本则需要 73 秒。
现在,理论上您可以在不使用单个循环的情况下完成此操作,如下所示:
eval_points = linspace(0,T_max,1000);
historic_weights = rand(100,1); % Note transposed from original
historic_times = rand(100,1);
eval_loop = reshape(0:T_max,1,1,[]); % size = [1,1,10000];
result = sum(historic_weight.*sum(Z_func(eval_points - historic_times + 1440*floor(historic_times/1440) - 1440*eval_loop, omega ),3),1);
但是,这将使用大量内存(>8 GB),因此对于您当前的情况可能不可行。我当前的机器上没有足够的内存来测试它,所以我不知道它的运行速度有多快,但理论上它应该更快,因为代码中缺少任何 for 循环。
关于matlab - 在 Matlab/Python 中向量化多个 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58027274/
在 Matlab 中,您可以选择创建新的“示例”脚本文件以及脚本、函数、类等。创建它们时,它们会获得一个脚本图标。 它们与其他标准脚本文件的处理方式有何不同? 是否有关于这些示例脚本类型的预期用途的文
我正在运行一个不是我自己编写的大 m 文件,它依赖于某些子函数。我想知道是否在所有嵌套函数的任何地方都使用了特定函数(在我的例子中是函数 eig.m(计算特征值))。有没有快速的方法来做到这一点? 亲
Matlab中有一个函数叫 copulafit .我怎样才能看到这个函数背后的代码?许多 Python 的 numpy 和 scipy 函数在 Github 上很容易开源,但由于某种原因我在 Gith
我定义了一个抽象基类measurementHandler < handle它定义了所有继承类的接口(interface)。这个类的两个子类是a < measurementHandler和 b < me
假设有一个矩阵 A = 1 3 2 4 4 2 5 8 6 1 4 9 例如,我有一个 Vector 包含该矩阵每一列的“类”
我有一个在后台运行的 Matlab 脚本。随着计算的进行,它会不断弹出进度栏窗口。这很烦人。 问题是我没有自己写 Matlab 脚本,这是一段很长很复杂的代码,我不想搞砸。那么如何在不修改 Matla
有没有办法从一个 matlab 程序中检测计算机上正在运行多少个 matlab 进程? 我想要恰好有 n 个 matlab 进程在运行。如果我的数量太少,我想创建它们,如果数量太多,我想杀死一些。您当
我正在测试我们在 Matlab 中开发的一个独立应用程序,当时我注意到它的内存使用量(根据 Windows 任务管理器)达到了 16gb 以上的数倍峰值。我决定在编译版本后面的脚本上使用 profil
我面临着一个相当棘手的问题。在 Matlab 中,命令 S = char(1044) 将俄语字母 д 放入变量 S。但是 disp(S) 返回空白符号,尽管内容实际上是正确的: >> S = char
我在这行 MATLAB 代码中遇到内存不足错误: result = (A(1:xmax,1:ymax,1:zmax) .* B(2:xmax+1,2:ymax+1,2:zmax+1) +
我正在寻找一种在 MATLAB 中比较有限顺序数据与非确定性顺序的方法。基本上,我想要的是一个数组,但不对包含的元素强加顺序。如果我有对象 a = [x y z]; 和 b = [x z y]; 我希
我有一个由 1 和 0 组成的二维矩阵。 mat = [0 0 0 0 1 1 1 0 0 1 1 1 1 1 0 0 1 0 0 0 1 0 1 1 0 0 1]; 我需
我可以在 Matlab 中用一组 x,y 点绘制回归线。但是,如果我有一组点(如下图),假设我有四组点,我想为它们绘制四条回归线……我该怎么做?所有的点都保存在 x,y 中。没有办法将它们分开并将它们
我正在尝试使用以下代码在 MATLAB 中绘制圆锥体。但是,当 MATLAB 生成绘图时,曲面中有一个间隙,如下图所示。谁能建议关闭它的方法? clearvars; close all; clc; [
我有一个 map称为 res_Map,包含一组不同大小的数组。我想找到用于存储 res_Map 的总内存。 正如您在下面看到的,看起来 res_Map 几乎不占用内存,而 res_Map 中的各个元素
有没有办法在 MATLAB 中组合 2 个向量,这样: mat = zeros(length(C),length(S)); for j=1:length(C) mat(j,:)=C(j)*S;
已结束。此问题不符合 Stack Overflow guidelines 。它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答它。 关闭 5 年前
我正在尝试将MatLab中的t copula适配到我的数据,并且我的功能是: u = ksdensity(range_1, range_1,'function','cdf'); v = ksdens
大家好,我目前正在尝试使用论文“多尺度形态学图像简化”中的 SMMT 运算符 Dorini .由于没有订阅无法访问该页面,因此我将相关详细信息发布在这里: 请注意,我将相关文章的部分内容作为图片发布。
我在MATLAB中编写代码,需要使用一个名为modwt的函数,该函数同时存在于两个我同时使用的工具箱(Wavelet和WMTSA)中。问题在于,一个版本仅返回一个输出,而另一个版本则返回三个输出。我应
我是一名优秀的程序员,十分优秀!