For loops are too slow
20 次查看(过去 30 天)
显示 更早的评论
I have some MATLAB code that was given to me by a coworker. This code contains a three layered for loop. So the code has the basic structure:
for
for
for
The code works but it takes an incredibly long time to execute as the three layered for loop is performing operations on a 40000x4 matrix. I need a way to either bypass the for loops or make them run faster. Someone mentioned to me that I could call Perl in MATLAB to do this but I am unsure how this works. Dose anyone have any ideas. Thank you.
3 个评论
Matt Kindig
2013-8-7
I think the pre-allocating xmsd would help streamline the code, while still retaining the for loops. Something like this:
xmsd = NaN(nmax, 3);
for i = 1:1:N
for j = 1:1:norigin
jstart = (j-1)*N + i;
for k = nmin:1:nmax
kend = jstart + k*N;
xmsd(k,1:3) = xmsd(k,1:3) + (md_msd(kend,1:3) - md_msd(jstart,1:3) ).^2;
end
end
end
采纳的回答
dpb
2013-8-7
编辑:dpb
2013-8-8
for i = 1:1:N
for j = 1:1:norigin
jstart = (j-1)*N + i;
for k = nmin:1:nmax
kend = jstart + k*N;
xmsd(k,1:3) = xmsd(k,1:3) + (md_msd(kend,1:3)-md_msd(jstart,1:3)).^2;
end
end
end
Start in the inner loop...first off
md_msd(jstart,1:3)
is invariant w/ k so replace the indexed expression w/ the equivalent
...
for j = 1:1:norigin
jstart = (j-1)*N + i;
mdj=md_msd(jstart,1:3);
for k = nmin:nmax
kend = jstart + k*N;
xmsd(k,1:3) = xmsd(k,1:3) + (md_msd(kend,1:3)-mdj).^2;
end
end
Then, kend takes on values of jstart+N, jstart+2N, jstart+3N, ... so define an index vector as
kdx=jstart+N:N:jstart+nmax*N;
and then the loop on k can be written as
xmsd(nmin:nmax,1:3) = xmsd(nmin:nmax,1:3) + (md_msd(kdx,1:3)-mdj).^2;
So, after the first loop reduction you're left with
for i = 1:1:N
for j = 1:1:norigin
jstart = (j-1)*N + i;
kdx=jstart+nmin*N:N:jstart+nmax*N;
mdj=md_msd(jstart,1:3);
xmsd(nmin:nmax,1:3) = xmsd(nmin:nmax,1:3) + (md_msd(kdx,1:3)-mdj).^2;
end
end
Now, see what you can do from here... :)
ADDENDUM: Forgot to add -- xmsd IS preallocated, correct????
ERRATUM: "Then, kend takes on values of jstart+N, jstart+2N, jstart+3N, ..."
Actually, the first value is jstart+nmin*N not jstart+N. The increment is N, however, so only the lower bound needs must be corrected.
更多回答(0 个)
另请参阅
类别
在 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!