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
2011-12-15
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 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!