How to reduce number of loops
Show older comments
Hello,
I have code, which assembles coefficients into large sparse matrix. My coding seems to be inefficient as I use 3 loops, so I'm trying to improve the code to get better pefromance. I think it would be nice to reduce the number of loops to single loop and run it parallel (parfor), but I'm struggling to do that..
The main matrix sig is a matrix of size LX,LY,LZ and vectors dx, dy, dz holds the distances between nods along the diretion X, Y an Z.
for i = 2:LX-1
for j = 2:LY-1
for k = 2:LZ-1
Ctop(i,j,k) = (-1/dz(k-1))*((sig(i-1,j ,k-1)*((dx(i-1)*dy(j ))/4))+...
(sig(i ,j ,k-1)*((dx(i )*dy(j ))/4))+...
(sig(i-1,j-1,k-1)*((dx(i-1)*dy(j-1))/4))+...
(sig(i ,j-1,k-1)*((dx(i )*dy(j-1))/4)));
end
end
end
C1v=reshape(Ctop,[],1);
thanks for help!
4 Comments
Cedric
on 19 Apr 2013
What would be typical values for LX, LY, LZ? Is it normal that upper boundaries are {LX,LY,LZ}-1 when you use no {i,j,k}+1 in you computation? It means that Ctop(LX,:,:), Ctop(:,LY,:), and Ctop(:,:,LZ) are not defined. Do you prealloc Ctop before the triple loop?
David Kusnirak
on 19 Apr 2013
Cedric
on 19 Apr 2013
And actually this is correct that Ctop has the size of sig and that Ctop(LX,:,:), Ctop(:,LY,:), Ctop(:,:,LZ), Ctop(1,:,:), Ctop(:,1,:), and Ctop(:,:,1) are 0's ?
David Kusnirak
on 19 Apr 2013
Accepted Answer
More Answers (0)
Categories
Find more on Fuzzy Logic Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!