为MATLAB中的分布族提高NORMRND的速度 [英] Improve speed of NORMRND for a family of distributions in MATLAB

查看:90
本文介绍了为MATLAB中的分布族提高NORMRND的速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在寻找一种加快代码速度的方法.我有一个大的正态分布向量(即均值和标准差向量),需要从中产生随机数.我的代码的通用示例如下:

So, I am looking for a way to speed up my code. I have a large vector of normal distributions (i.e. a vector of means and standard deviations) that I need to generate random numbers from. A generic example of my code looks like this:

tic

N=1e6;
mu = rand(N,1);
sigma = rand(N,1);

temp = zeros(length(mu),1);

for i = 1:length(mu)
     temp(i) = normrnd(mu(i),sigma(i));
end

toc

此代码以其当前形式具有的经过时间:

This code in its current form has an elapsed time of:

Elapsed time is 12.281509 seconds.

我通常会尝试对大多数计算密集型命令进行矢量化处理,但是现在我对如何使其运行更快感到困惑.每次运行代码时,我将不得不多次执行此操作,所以我可以使其变得越快越好.

I normally try to vectorize most of my computationally intensive commands, but right now I am stumped as to how I can make this run faster. I will have to perform this operation multiple times every time that the code is run, so the faster I can make it the better.

你们中的任何MATLAB天才都对如何加快速度有任何想法吗?

Do any of you MATLAB geniuses out there have any thoughts of how to speed this up?

谢谢!约翰

推荐答案

绑定到normrnd.m中以获取此定制代码,该代码必须复制问题中描述的功能-

Hacked into normrnd.m to get this customized code that must replicate the functionality depicited in the problem -

N=1e6;
mu = rand(N,1);
sigma = rand(N,1);
temp = randn(size(sigma)).*sigma + mu;

在我的系统上,运行时间从18.946094 seconds减少到0.037229 seconds.

On my system, the runtime was reduced from 18.946094 seconds to 0.037229 seconds.

希望这对您有用!

这篇关于为MATLAB中的分布族提高NORMRND的速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆