2016-04-13 15 views
2

Matlabでは、いくつかの要素がゼロに設定されている行列があります。例:0でないすべての行列要素について、ゼロでない最も近い要素のインデックスを探したいとします。複数のインデックスが可能な場合は、それらのすべてを返す必要があります。ここでは賢明な解決策がありますが、どのようにfor-loopのものをたくさん避けることができますか?Matlab - ゼロ行列要素ごとに最も近い非ゼロ要素のインデックスを見つける

M = [ 
    0 1 0 0 0 
    2 5 0 3 0 
    0 0 0 0 0 
    0 5 0 2 1 
]; 
[D,IDX] = bwdist(M~=0) 

結果:

+0

'diff'が有用であり得ます。 –

+1

あなたは一番近いと思いますか?縦、横、斜めに? – DeeCee

+0

@dean_sh:あなたが列挙したすべての指示を考慮する必要があります。 –

答えて

4

ありdistance transform計算IPTで効率的bwdist function

D = 
    1.0000   0 1.0000 1.0000 1.4142 
     0   0 1.0000   0 1.0000 
    1.0000 1.0000 1.4142 1.0000 1.0000 
    1.0000   0 1.0000   0   0 

IDX = 
      2   5   5   14   14 
      2   6   6   14   14 
      2   6   6   14   20 
      8   8   8   16   20 

ザ・はIDX線形インデックスMで最も近いゼロ以外の値のが含まれています返さ。要素ごとに1つのインデックスのみを返します。ここ

+1

距離変換はこれを行うための第1の方法です...特に、ブルートフォースではない非常に効率的な実装があるためです。私の+1。 – rayryeng

3

は、セル内の各ゼロ要素各々について非ゼロに最も近い要素(ユークリッド距離)のインデックスを格納するベクトルbsxfunを用いたアプローチとmat2cellだ -

%// Assuming A as the input matrix. Store rows, columns of zero and non-zeros 
[rz,cz] = find(A==0); 
[rnz,cnz] = find(A~=0); 

%// Store zero pt indices 
zero_pts = [rz cz]; 

%// Get squared euclidean distances 
dists = bsxfun(@minus,rnz,rz.').^2 + bsxfun(@minus,cnz,cz.').^2; 

%// Get all nearest XY indices of nonzeros for each zero pt 
[R_idx,C_idx] = find(bsxfun(@eq,min(dists,[],1),dists)); 
idx = [rnz(R_idx) cnz(R_idx)]; 

%// Cut at each shifting positions and thus create a cell array, with a 
%// cell of indices of non-zero nearest elements for each zero element 
nearest_nnonzero_pts = mat2cell(idx,histc(C_idx,1:max(C_idx))) 

サンプル入力、出力 -

入力:

>> A 
A = 
    0  1  0  0  0 
    2  5  0  3  0 
    0  0  0  0  0 
    0  5  0  2  1 

出力(ゼロ点):

>> disp(zero_pts) 
    1  1 
    3  1 
    4  1 
    3  2 
    1  3 
    2  3 
    3  3 
    4  3 
    1  4 
    3  4 
    1  5 
    2  5 
    3  5 

出力(最も近い非ゼロの対応点):

>> celldisp(nearest_nnonzero_pts) 
nearest_nnonzero_pts{1} = 
    2  1 
    1  2 
nearest_nnonzero_pts{2} = 
    2  1 
nearest_nnonzero_pts{3} = 
    4  2 
nearest_nnonzero_pts{4} = 
    2  2 
    4  2 
nearest_nnonzero_pts{5} = 
    1  2 
nearest_nnonzero_pts{6} = 
    2  2 
    2  4 
nearest_nnonzero_pts{7} = 
    2  2 
    4  2 
    2  4 
    4  4 
nearest_nnonzero_pts{8} = 
    4  2 
    4  4 
nearest_nnonzero_pts{9} = 
    2  4 
nearest_nnonzero_pts{10} = 
    2  4 
    4  4 
nearest_nnonzero_pts{11} = 
    2  4 
nearest_nnonzero_pts{12} = 
    2  4 
nearest_nnonzero_pts{13} = 
    4  5 
関連する問題