向量化 for 循环

发布于 2024-11-03 18:30:29 字数 758 浏览 2 评论 0原文

在 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

注意 wu_diffsdrank_indfr_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盗梦空间 2024-11-10 18:30:29

让我看看我的理解是否正确:只有当 u_diff 中没有对应的值小于D*sdw 高于某个阈值,对吗?

在这种情况下,您可以按以下方式重写代码:

%# find where u_diff is larger than D*sd
%# but not where there's any u_diff that is smaller than D*sd
whereToCopy = any( w(:,:,rank_ind) >= thresh  & ...
  abs(u_diff(:,:,rank_ind)) > D*sd(:,:,rank_ind),3) & ...
  ~any( w(:,:,rank_ind) >= thresh  & ...
  abs(u_diff(:,:,rank_ind)) <= D*sd(:,:,rank_ind),3);

%# whereToCopy has 1 wherever we need to copy, and 0 elsewhere
%# thus, create fg from fr_bw directly by multiplication
fg = double(fr_bw) .* double(whereToCopy);

Let me see whether I understand you correctly: You want to copy the value of fr_bw into fg only if no corresponding value in u_diff is smaller than D*sd, and w is above some threshold, right?

In this case, you can rewrite your code the following way:

%# find where u_diff is larger than D*sd
%# but not where there's any u_diff that is smaller than D*sd
whereToCopy = any( w(:,:,rank_ind) >= thresh  & ...
  abs(u_diff(:,:,rank_ind)) > D*sd(:,:,rank_ind),3) & ...
  ~any( w(:,:,rank_ind) >= thresh  & ...
  abs(u_diff(:,:,rank_ind)) <= D*sd(:,:,rank_ind),3);

%# whereToCopy has 1 wherever we need to copy, and 0 elsewhere
%# thus, create fg from fr_bw directly by multiplication
fg = double(fr_bw) .* double(whereToCopy);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文