我需要尽快计算矩阵查找(见下文)。
totquadpoint和nbval.Wepast是每个大约200个固定标量(向量flnewcumyieldpos和flneweta1pos的维度)。 nexttsteppenal是一个3D矩阵(尺寸至少为80 * 80 * 80)
由于我需要多次执行此操作(此双循环本身处于三重嵌套循环中),我需要快速完成。有没有办法让它比下面的内容更快?如果在Matlab中不可能,如果我尝试在Java / C中编写那段代码,我可以期待什么样的增益?
--------代码:-----------------
for k=1:totquadpoint
nexttsteppenalYA1dd=nexttsteppenal(flnewcumyieldpos(k),flneweta1pos(k),:);
nexttsteppenalYA1du=nexttsteppenal(upflnewcumyieldpos(k),flneweta1pos(k),:);
nexttsteppenalYA1ud=nexttsteppenal(flnewcumyieldpos(k),upflneweta1pos(k),:);
nexttsteppenalYA1uu=nexttsteppenal(upflnewcumyieldpos(k),upflneweta1pos(k),:);
flnewportvalposvec=flnewportvalpos(k,:);
upflnewportvalposvec=upflnewportvalpos(k,:);
for we=1:nbval.Wepast
penddd(k,we)=nexttsteppenalYA1dd(flnewportvalposvec(we));
penddu(k,we)=nexttsteppenalYA1dd(upflnewportvalposvec(we));
pendud(k,we)=nexttsteppenalYA1du(flnewportvalposvec(we));
penduu(k,we)=nexttsteppenalYA1du(upflnewportvalposvec(we));
penudd(k,we)=nexttsteppenalYA1ud(flnewportvalposvec(we));
penudu(k,we)=nexttsteppenalYA1ud(upflnewportvalposvec(we));
penuud(k,we)=nexttsteppenalYA1uu(flnewportvalposvec(we));
penuuu(k,we)=nexttsteppenalYA1uu(upflnewportvalposvec(we));
end
end
答案 0 :(得分:0)
此:
for w=1:n
p(k,w)=d(f(w));
end
应该是:
p(k,1:n)=d(f(1:n));
甚至更好,当n
是f
的长度时:
p(k,:)=d(f);
对于外部循环,您可能需要sub2ind()
。
答案 1 :(得分:0)
以上是上述代码的可读版本,其中包含表格的模拟值。 我的问题是如何在最后优化两个嵌套for循环的速度(我需要运行这个循环可能一百万次)。
quadpoints=800;
nbWe=100;
penddd=zeros(quadpoints,nbWe);
penddu=zeros(quadpoints,nbWe);
pendud=zeros(quadpoints,nbWe);
penduu=zeros(quadpoints,nbWe);
penudd=zeros(quadpoints,nbWe);
penudu=zeros(quadpoints,nbWe);
penuud=zeros(quadpoints,nbWe);
penuuu=zeros(quadpoints,nbWe);
nsp=rand(100,50,100);
ypos=min(sort(poissrnd(50,quadpoints,1),'ascend'),100);
uypos=min(ypos,100);
etapos=min(sort(poissrnd(25,quadpoints,1),'ascend'),50);
uetapos=min(etapos,50);
Vpos=min(sort(poissrnd(50,quadpoints,nbWe),'ascend'),100);
uVpos=min(Vpos,100);
for k=1:quadpoints
nspdd=nsp(ypos(k),etapos(k),:);
nspdu=nsp(uypos(k),etapos(k),:);
nspud=nsp(ypos(k),uetapos(k),:);
nspuu=nsp(uypos(k),uetapos(k),:);
dVvec=Vpos(k,:);
uVvec=uVpos(k,:);
for we=1:nbWe
penddd(k,we)=nspdd(dVvec(we));
penddu(k,we)=nspdd(uVvec(we));
pendud(k,we)=nspdu(dVvec(we));
penduu(k,we)=nspdu(uVvec(we));
penudd(k,we)=nspud(dVvec(we));
penudu(k,we)=nspud(uVvec(we));
penuud(k,we)=nspuu(dVvec(we));
penuuu(k,we)=nspuu(uVvec(we));
end
end