2016-04-05 17 views
1

イメージ内のピクセルの輝度差に基づいて隣接リストを作成しています。 次のようにMatlabのコードスニペットは次のとおりです。matlabの入れ子になったforループを避けるには?

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 

は、どのように私はループのためにこれらの厄介な入れ子のを避けることができますか?画像サイズが大きければ、それは災害と同様に機能します。もし誰かが解決策を持っていれば、私にとって大きな助けになるでしょう。 よろしくお願いいたします Ratna

+2

やあ、と歓迎は、......この種の問題のための最初のスタンダールanwserはhttp://ch.mathworks.com/([ベクトル化]になりますhelp/matlab/matlab_prog/vectorization.html)(いくつかの[トリックス](http://www-h.eng.cam.ac.uk/help/tpl/programs/Matlab/tricks.html)) –

+0

'cur_label'と' indx_intensity'は何ですか? 'len'のサイズが' len'の正方形のイメージですか? – Ratbert

+0

cur_labelは、画像から1つのクラスタであるベクトルです(120x1とは、隣接行列に対して120ピクセルまたは頂点があることを意味します)。 indx_intensityは、それらのピクセルのグレー値(120グレー値)です。 120の頂点があるので、隣接行列は120x120のサイズになります。 –

答えて

0

ここでは入力indx_intensity1Dと仮定しています。仮定して、ここで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]; 
+0

対角要素 'if(k〜= j)'をどう避けますか? – kkuilla

+0

ああ、大丈夫です。私はそれを逃した..謝罪... – kkuilla

+0

@kkuillaああ大丈夫です。 'k〜= j'は' i's'と 'j's'をまたいだ2Dマスク配列の対角要素をスキップすることを意味していました。ここで 'i'と' j'は両方とも 'indx_intensity'の長さを表します。この対角要素の設定は 'mask(1:len + 1:end)= 0'で行われます。これは意味があると思っています:) – Divakar

関連する問題