How to simplify this piece of code?
3 次查看(过去 30 天)
显示 更早的评论
I am working on implementing the PAC2002 Magic Formula on MATLAB, and while I was writing the code for computing all the matrices of the model, I noticed the computing time required was getting way too big. I know this is happening because of so many variables I'm using in order to compute these matrices, but I don't know how to make it simpler really. Let me show an example:
Thetax = zeros(length(sigmax),length(Bx),length(Ex));
for i = 1:1:length(sigmax)
for j = 1:1:length(Bx)
for k = 1:1:length(Ex)
Thetax(i,j,k) = Cx*atan(Bx(j,k)*sigmax(i,j)-Ex(i,j)*(Bx(j,k)*sigmax(i,j)-atan(Bx(j,k)*sigmax(i,j))));
end
end
end
where length(sigmax) = 201, length(Bx) = 10, length(Ex) = 10.
I should mention that sigmax, Bx and Ex are not just vectors, they are 2 dim matrices. But this didn't affect much the computing time. It was the last equation that really did. This equation in particular increased so much the computing time, from just a few seconds to a few minutes. Everyt time I had to compute a matrix, it would be depending on 2 variables, and so I'd use some code like this:
Svx= zeros(length(Fz),length(dfZ)):
for i = 1:1:length(Fz)
for j = 1:1:length(dfZ)
Svx(i,j) = Fz(i)*(PVX1+PVX2*dfZ(j))*LVX*LMUX;
end
end
The problem is that almost every independent variable in these equations is depending on other 2, making themselves 2nd dim matrices, and so I have lots of pieces of code in my script that look like the last equation. By the time I added the 1st equation, the computing time just went to the moon! Is there any way around this problem, that does not require calling so many fors?
Thank you.
1 个评论
Adam
2015-3-25
If you have 2d matrices I would not recommend using e.g.
length( sigmax )
You should use the size operator with an explicit dimension to avoid unwanted surprises in some cases.
采纳的回答
Roger Stafford
2015-3-25
编辑:Roger Stafford
2015-3-25
You are concentrating too much on getting rid of the for-loops. You should look for unnecessarily repeated operations, whether in for-loops or elsewhere. For example, in your second case the line
Svx(i,j) = Fz(i)*(PVX1+PVX2*dfZ(j))*LVX*LMUX;
requires five additions or multiplications for each step in the loops, a total of 5*length(Fz)*length(dfZ) operations. There is unnecessary repetition here. If you write it like
T = (PVX1+PVX2*dfZ)*(LVX*LMUX);
and then inside the loops:
for
for
Svx(i,j) = Fz(i)*T(j);
there is a total of only 3*length(dfZ) + 1*length(Fz)*length(dfZ) operations. By storing results in T, you have cut down the number of required operations.
Actually this code can be vectorized. Assuming, say, that Fz and dfZ are column vectors, you could write:
Svx = Fz*((PVX1+PVX2*dfZ)*(LVX*LMUX)).';
In the first code you might gain something by storing a temporary product:
t = Bx(j,k)*sigmax(i,j);
Thetax(i,j,k) = Cx*atan(t-Ex(i,j)*(t-atan(t)));
which cuts down the number of multiplications. However, the numerous calls on atan are what is taking the majority of execution time and I don't see a way of avoiding this.
0 个评论
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Variable Initialization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!