Nested loops

Here's a tricky one! How can I get rid of the loops?
for pix = 1:NumPixels
for ji = min(ShellNumbers(:,pix)):max(ShellNumbers(:,pix))
for jj = min(AngleNumbers(:,pix)):max(AngleNumbers(:,pix))
pos = ShellNumbers(:,pix) == ji & AngleNumbers(:,pix) == jj;
W(ji,jj) = W(ji,jj) + sum(PathLengths(pos,pix));
L(ji,jj,1,pix) = sum(PathLengths(pos,pix));
clear pos
end
end
end

답변 (1개)

Jan
Jan 2011년 12월 15일

0 개 추천

I cannot omit the loops, but it can be made faster:
% Pre-allocate!
s1 = max(ShellNumbers(:));
s2 = max(angleNumbers(:));
W = zeros(s1, s2);
L = zeros(s1, s2, 1, NumPixels); % What is "i"?!
for pix = 1:NumPixels
jjLow = min(AngleNumbers(:,pix));
jjHigh = max(AngleNumbers(:,pix));
for ji = min(ShellNumbers(:,pix)):max(ShellNumbers(:,pix))
a = ShellNumbers(:,pix) == ji;
for jj = jjLow:jjHigh
pos = a & AngleNumbers(:,pix) == jj;
B = sum(PathLengths(pos,pix));
W(ji,jj) = W(ji,jj) + B;
L(ji,jj,i,pix) = B; % What is "i"?!
end
end
end

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2011년 12월 15일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by