How to reduce number of loops
이전 댓글 표시
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
Cedric
2013년 4월 19일
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
2013년 4월 19일
Cedric
2013년 4월 19일
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
2013년 4월 19일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Fuzzy Logic Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!