计算彼此之间的距离不超过某个常数的点的索引矩阵

Compute matrix of indices of points whose distance between each other does not exceed some constant

考虑一个大小为 nxn 的方阵 D,它表示 n 个点之间的成对距离,即 D(1, 2) 是点 1 和点 2 之间的距离,或者, 一般来说,D(i, j) 是点 i 和 j 之间的距离。

我想计算另一个大小为 mx2 的矩阵 L,其索引 (i, j) 的 m 个点彼此之间的距离不超过某个常数 w。

例如,给定 D

octave:1> D = [1 2 3; 4 5 6; 7 8 9]
D =

   1   2   3
   4   5   6
   7   8   9

对于 w = 4 我想要 L 中距离不超过 w = 4 的点的索引 (i,j)

octave:2> L = [1 2; 2 2; 3 2; 1 3; 2 3; 3 3]
L =

   1   2
   2   2
   3   2
   1   3
   2   3
   3   3

作为第一个近似值,我相当天真地做到了这一点

D(D > w) = 0;

L = [];
for i = 1:size(D,1)
  for j = 1:size(D,2)
    if D(i, j) != 0
      L = [L; i j];
    end
  end
end

是否有更快(强调更快)或更简单的方法来做到这一点?我对MATLAB不是很熟悉

使用find:

D = [1 2 3; 4 5 6; 7 8 9];
w = 4;
[I, J] = find(D <= w);
L = [I J];