- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个代码,旨在模拟理想气体在盒子中的运动。它基于蒙特卡罗模拟中的大都会方法。但是,我使用了一系列逻辑语句(主要是 ifs)来定义边界条件,以找到与随机选择的粒子相邻的粒子。该算法为 1x4 矩阵内的任何相邻粒子表示 1,为没有粒子的相邻点表示 0。我需要算法自动将框外的任何相邻点设置为 0,以用于框边缘上的任何粒子。有没有办法减少这些逻辑语句并仍然得到相同的结果?代码如下。
% define constants
beta = .01; %Inverse temperature
N=2000; %Duration of simulation
eps = -7;
mu=9;
for j=1:N
a=randi(L);
b=randi(L);
c=randi(L);
d=randi(L);
% Calculate energy at positions
if lattice(a,b)==1 && lattice(c,d)==0
%If distribution (according to energy) suggests move,
lattice(a,b)=0;
lattice(c,d)=1;
%Energy at random site/position
E = n*(mu-eps)
if (a~=1&& a~=L &&b~=1 && b~=L)
adjacent= [lattice(a-1,b) lattice(a+1,b) lattice(a,b+1) lattice(a,b-1) 1];
else if (a==1 && b==1)
adjacent= [0 lattice(a+1,b) lattice(a,b+1) 0 1];
else if (a==L && b==L)
adjacent= [lattice(a-1,b) 0 0 lattice(a,b-1) 1];
else if(a==1&&b==L)
adjacent= [0 lattice(a+1,b) 0 lattice(a,b-1) 1];
else if (a==L &&b==1)
adjacent= [lattice(a-1,b) 0 lattice(a,b+1) 0 1];
else if (a==1 && b~=L && b~=1)
adjacent= [0 lattice(a+1,b) lattice(a,b+1) lattice(a,b-1) 1];
else if (a==L && b~=L && b~=1)
adjacent= [lattice(a-1,b) 0 lattice(a,b+1) lattice(a,b-1) 1];
else if (b==1&&a~=L&&a~=1)
adjacent= [lattice(a-1,b) lattice(a+1,b) lattice(a,b+1) 0 1];
else if (b==L&&a~=L&&a~=1)
adjacent= [lattice(a-1,b) lattice(a+1,b) 0 lattice(a,b-1) 1];
end
end
end
end
end
end
end
end
end
E1 = mu*sum(adjacent) + eps*sum(sum(adjacent.*adjacent));
%This calculates the energy of the particle at its current
%position
if (c~=1&& c~=L &&d~=1 && d~=L)
adjacent1= [lattice(c-1,d) lattice(c+1,d) lattice(c,d+1) lattice(c,d-1) 1];
else if (c==1 && d==1)
adjacent1= [0 lattice(c+1,d) lattice(c,d+1) 0 1];
else if (c==L && d==L)
adjacent1= [lattice(c-1,d) 0 0 lattice(c,d-1) 1];
else if(c==1&&d==L)
adjacent1= [0 lattice(c+1,d) 0 lattice(c,d-1) 1];
else if (c==L &&d==1)
adjacent1= [lattice(c-1,d) 0 lattice(c,d+1) 0 1];
else if (c==1 && d~=L && d~=1)
adjacent1= [0 lattice(c+1,d) lattice(c,d+1) lattice(c,d-1) 1];
else if (c==L && d~=L && d~=1)
adjacent1= [lattice(c-1,d) 0 lattice(c,d+1) lattice(c,d-1) 1];
else if (d==1&&c~=L&&c~=1)
adjacent1= [lattice(c-1,d) lattice(c+1,d) lattice(c,d+1) 0 1];
else if (d==L&&c~=L&&c~=1)
adjacent1= [lattice(c-1,d) lattice(c+1,d) 0 lattice(c,d-1) 1];
end
end
end
end
end
end
end
end
end
E2 = mu*sum(adjacent) + eps*sum(sum(adjacent1.*adjacent1));
%Calculates the energy at randomly chosen position.
dE = E2-E1; %Change in energy of the particle as it goes from the two locations.
if rand<exp(-beta*dE)
lattice(a,b)=0;
lattice(c,d)=1;
end
end
time(:,:,j)=lattice;
end
最佳答案
您可以取消所有这些 if else
语句。实际上,您只是在评估四个逻辑表达式并基于此选择索引。您可以使用以下内容
ab = [a-1, b; a+1, b; a, b+1; a, b-1];
abIn = [a ~= 1; a ~= L; b ~= L; b ~= 1];
adjacent = zeros(1, 5);
adjacent(abIn) = lattice(sub2ind([L, L], ab(abIn, 1), ab(abIn, 2)));
adjacent(5) = 1;
而不是第一组 if else
语句,并为第二组更改名称。这里的想法是只获取边界内的 lattice
值。四个逻辑表达式在这里用于 logical indexing在第四行。
关于algorithm - MATLAB:提高逻辑语句中的代码效率(Metropolis 方法),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23165588/
我目前在实现 Metropolis-Hastings 算法时遇到问题。 我正在尝试使用该算法来计算形式的积分 在使用这个算法时,我们可以获得一长串配置(在这种情况下,每个配置只是一个数字),这样在链的
我是一个统计学外行,在阅读了一些关于 Metropolis-Hastings MCMC 算法的博客和示例之后,我有一个问题: 在M-H算法中,一个新样本(比如,x')是否被接受取决于“接受概率”,al
MCMC是从复杂概率模型中采样的通用技术。 蒙特卡洛 马尔可夫链 Metropolis-Hastings算法 问题 如果需要计算有复杂后验pdf p(θ|
我正在尝试在 C 中实现 Metropolis-Hastings 算法以实现简单的线性回归(不使用其他库(boost、Eigen 等)并且不使用二维数组)*。为了更好地测试代码/跟踪图的评估,我通过保
每当我制作Metropolis UI项目并尝试将Label字体更改为除白色以外的任何颜色时,都不会发生任何事情,并且在编译项目时颜色始终为白色, 那么,有什么可以改变颜色的字体吗? 我正在使用Delp
我目前正在用 C 编写一些用于模拟退火的代码。我在使用此函数时遇到了问题: int metrop (E, E1, T) { int j; if (exp((-E-E1)/T) > ran(
我正在尝试使用 Metropolis-Hastings 进行贝叶斯回归。测试数据生成如下(Python代码,我没有复制整个代码): trueA = 5 ; trueB = 7 ;trueSD = 10
我一直在阅读 Metropolis-Hastings (MH) 算法。从理论上讲,我了解该算法的工作原理。现在,我正在尝试使用 python 实现 MH 算法。 我遇到了以下 notebook .它完
我有一个代码,旨在模拟理想气体在盒子中的运动。它基于蒙特卡罗模拟中的大都会方法。但是,我使用了一系列逻辑语句(主要是 ifs)来定义边界条件,以找到与随机选择的粒子相邻的粒子。该算法为 1x4 矩阵内
测试平台目标的不同选项我在 delphi xe5 帮助中找到了以下信息: Note: You cannot set OS X as the target platform for a Metropol
我正在尝试编写一个基于 Haskell 中的 Metropolis 算法的 MCMC 程序,但我在从概率分布中采样(生成伪随机数)和构建程序时遇到问题。目前,我很高兴使用带有硬编码种子的生成器,而不是
我正在尝试编写一个基于 Haskell 中的 Metropolis 算法的 MCMC 程序,但我在从概率分布中采样(生成伪随机数)和构建程序时遇到问题。目前,我很高兴使用带有硬编码种子的生成器,而不是
我正在尝试使用带有 R 的 MH 算法来实现一个简单的 MCMC,问题是我得到了这个错误(我试图计算 alpha 并且它不是 NA 问题) Error in if (runif(1) 0){
我有一个相对简单的函数,具有三个未知输入参数,我只知道它们的上限和下限。我也知道我所有数据的输出 Y 应该是什么。 到目前为止,我已经在 python 中完成了一个简单的网格搜索,遍历所有可能的参数组
我一直在运行一些相当昂贵且老化时间较长的型号。我想保存并恢复 AdaptiveMetropolis 步骤方法的状态(至少)以减少老化时间。有没有推荐的方法来做到这一点,或者我应该腌制 step_met
我遇到了在我的应用程序中使用表单中的 TListBox 时,在 HorzScrollBox 中选择不正确的项目的问题。该表单上有两个(或更多)TListBox。它们彼此远离放置,使得它们的绝对左坐标差
我有一个结构如下图所示的模型: 我有几个人(在这张图片中索引为 1...5)。种群参数(A 和 B,但可以有更多)确定每个个体的潜在变量 L[i] 的分布。潜在变量 L[i] 以概率的方式确定观测值
我的 Metropolis-Hastings 问题具有平稳的二项分布,所有建议分布 q(i,j) 都是 0.5。引用绘图和直方图,算法是否应该如此明确地以 0.5 为中心,二项式分布的概率? pi <
我在使用基于 Metropolis-Hastings 算法的 MCMC 技术对后验分布进行采样(因此是贝叶斯方法)方面很新。 为此,我在 R 中使用了 mcmc 库。我的分布是多维的。为了检查这个me
我正在尝试用 Python 实现一个简单的蒙特卡洛(我对此还很陌生)。来自 C 我可能走的是最错误的道路,因为我的代码对于我所要求的来说太慢了:对于 60 个 3d 粒子和周期性边界条件(PBC),我
我是一名优秀的程序员,十分优秀!