Vectorize your code
Reduce reduce the number of for loops?
1 次查看(过去 30 天)
显示 更早的评论
I have function.
function G=J(Fleft,Fright,Data)
cxleft=size(Fleft,2);
cxright=size(Fright,2);
cd=size(Data,2);
k=1;
Tmp=zeros(3,cxright*cxleft*cd);
for i=1:cxleft
for j=1:cxright
for t=1:cd
Tmp(1,k)=Fleft(1,i)+Fright(1,j)+Data(1,t);
Tmp(2,k)=(Data(1,t)*Data(2,t)+Fleft(1,i)*Fleft(2,i)+Fright(1,j)*Fright(2,j))/Tmp(1,k);
Tmp(3,k)=((Fleft(1,i)*Fright(1,j)*(Fleft(2,i)-Fright(2,j))^2+Fleft(1,i)*Data(1,t)*(Fleft(2,i)-Data(2,t))^2+Fright(1,j)*Data(1,t)*(Fright(2,j)-Data(2,t))^2)/Tmp(1,k))+Fleft(3,i)+Fright(3,j)+Data(3,t);
k=k+1;
end
end
end
G=Tmp;
How do we reduce the number of for loops?
0 个评论
回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!