正确的方法当然是使用最近邻搜索算法.
但是,如果您的维度不太高并且数据集不大,那么您可以简单地使用bsxfun:
d = bsxfun( @minus, permute( bigList, [1 3 2] ), permute( littleList, [3 1 2] ) ); %//diff in third dimension
d = sum( d.^2, 3 ); %// sq euclidean distance
[minDist minIdx] = min( d, [], 2 );
除了提出的矩阵乘法方法之外here,还有另一种不带循环的矩阵乘法
nb = sum( bigList.^2, 2 ); %// norm of bigList's items
nl = sum( littleList.^2, 2 ); %// norm of littleList's items
d = bsxfun( @sum, nb, nl.' ) - 2 * bigList * littleList'; %// all the distances
该方法背后的观察是对于欧几里德距离(L2-范数)
|| a - b ||^2 = ||a||^2 + ||b||^2 - 2<a,b>
With <a,b>
是两个向量的点积。