向量化 for 循环
在 MATLAB 中,矢量化代码比使用 for 循环更快。我一直在尝试这样做,但我不完全理解它是如何工作的。我希望有人能向我展示如何通过改变索引来改进这两个 for 循环和一个 while 循环,以便我能够理解它。任何帮助都会很棒。
width= 700;
height= 600;
fg= zeros(height, width);
for i= 1: height
for j= 1: width
fg(i, j) = 0;
while ((match== 0)&& (k<= M))
if (w(i, j, rank_ind(k))>= thresh)
if (abs(u_diff(i, j, rank_ind(k)))<= D* sd(i, j, rank_ind(k)))
fg(i, j)= 0;
match= 1;
else
fg(i, j)= fr_bw(i, j);
end
end
k= k+ 1;
end
end
end
注意 w
、u_diff
、sd
、rank_ind
和 fr_b
都是数组
In MATLAB, vectorized code is faster than using for-loops. I have been trying to do this but I do not fully understand how this works. I was hoping someone could show me how to improve these 2 for loops and a while loop with changing indices so I could get my head round it. Any help would be a amazing.
width= 700;
height= 600;
fg= zeros(height, width);
for i= 1: height
for j= 1: width
fg(i, j) = 0;
while ((match== 0)&& (k<= M))
if (w(i, j, rank_ind(k))>= thresh)
if (abs(u_diff(i, j, rank_ind(k)))<= D* sd(i, j, rank_ind(k)))
fg(i, j)= 0;
match= 1;
else
fg(i, j)= fr_bw(i, j);
end
end
k= k+ 1;
end
end
end
Note w
, u_diff
, sd
, rank_ind
and fr_b
are all arrays
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让我看看我的理解是否正确:只有当
u_diff
中没有对应的值小于D*sd
和w
高于某个阈值,对吗?在这种情况下,您可以按以下方式重写代码:
Let me see whether I understand you correctly: You want to copy the value of
fr_bw
intofg
only if no corresponding value inu_diff
is smaller thanD*sd
, andw
is above some threshold, right?In this case, you can rewrite your code the following way: