如何避免在Matlab中嵌套for循环? [英] How to avoid nested for loops in matlab?

查看:530
本文介绍了如何避免在Matlab中嵌套for循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在基于图像中像素的强度差异构造邻接表. Matlab中的代码段如下:

m=1;
len = size(cur_label, 1);
for j=1:len
    for k=1:len
        if(k~=j)    % avoiding diagonal elements
            intensity_diff = abs(indx_intensity(j)-indx_intensity(k));     %intensity defference of two pixels.

            if intensity_diff<=10     % difference thresholded by 10
                adj_list(m, 1) = j;   % storing the vertices of the edge
                adj_list(m, 2) = k;
                m = m+1;
            end
        end
    end
end
y = sparse(adj_list(:,1),adj_list(:,2),1);       % creating a sparse matrix from the adjacency list

如何避免这些讨厌的嵌套循环?如果图像很大,则其工作就像灾难一样.如果有人有任何解决方案,那对我将是一个很大的帮助. 问候 拉特纳

解决方案

我在这里假设输入indx_intensity作为1D数组.在这种假设下,这是使用 broadcasting/bsxfun -的矢量化方法>

%// Threshold parameter
thresh = 10;

%// Get elementwise differentiation between elements in indx_intensity
diffs = abs(bsxfun(@minus,indx_intensity(:),indx_intensity(:).')) %//'

%// Threshold the differentiations against the threshold, thus giving us a 
%// 2D square matrix. Then, set the diagonal elements to zero to avoid them.
mask = diffs <= thresh;
mask(1:len+1:end) = 0;

%// Get the indices of the TRUE elements in the valid mask as final output.
[R,C] = find(mask);
adj_list_out = [C R];

I am constructing an adjacency list based on intensity difference of the pixels in an image. The code snippet in Matlab is as follows:

m=1;
len = size(cur_label, 1);
for j=1:len
    for k=1:len
        if(k~=j)    % avoiding diagonal elements
            intensity_diff = abs(indx_intensity(j)-indx_intensity(k));     %intensity defference of two pixels.

            if intensity_diff<=10     % difference thresholded by 10
                adj_list(m, 1) = j;   % storing the vertices of the edge
                adj_list(m, 2) = k;
                m = m+1;
            end
        end
    end
end
y = sparse(adj_list(:,1),adj_list(:,2),1);       % creating a sparse matrix from the adjacency list

How can I avoid these nasty nested for loops? If the image size is big, then its working just as disaster. If anyone have any solution, it would be a great help for me. Regards Ratna

解决方案

I am assuming the input indx_intensity as a 1D array here. With that assumption, here's a vectorized approach with broadcasting/bsxfun -

%// Threshold parameter
thresh = 10;

%// Get elementwise differentiation between elements in indx_intensity
diffs = abs(bsxfun(@minus,indx_intensity(:),indx_intensity(:).')) %//'

%// Threshold the differentiations against the threshold, thus giving us a 
%// 2D square matrix. Then, set the diagonal elements to zero to avoid them.
mask = diffs <= thresh;
mask(1:len+1:end) = 0;

%// Get the indices of the TRUE elements in the valid mask as final output.
[R,C] = find(mask);
adj_list_out = [C R];

这篇关于如何避免在Matlab中嵌套for循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆