Finding distances between each combination of A(i) and B(j). If you have the statistics toolbox, pdist2 does this for you:A=[ 1 2; 3 4];B=[1 3; 5 6; 2 1];dist = pdist2(A,B); 遍历A或B中的最小值(我取A,因为它在您的示例中为最小),并为A中的每个点查找其余集合中的最接近点B:Looping over the smallest of A or B (I'll take A, cause it is smallest in your example) and finding for each point in A the closest point in the remaining set of B:N = size(A,1);matchAtoB=NaN(N,1);for ii=1:N dist(:,matchAtoB(1:ii-1))=Inf; % make sure that already picked points of B are not eligible to be new closest point [~,matchAtoB(ii)]=min(dist(ii,:));endmatchBtoA = NaN(size(B,1),1);matchBtoA(matchAtoB)=1:N;remaining_indices = find(isnan(matchBtoA)); 将结果合并为所需的输出矩阵C和D:C=arrayfun(@(ii) [A(ii,:) ; B(matchAtoB(ii),:)],1:N,'uni',false);D=mat2cell(B(remaining_indices,:),ones(numel(remaining_indices),1),size(B,2));请注意,此代码也可用于1D点或更高的点(N-D),pdist2将所有内容展平为标量距离.Note that this code will also work with 1D points or higher (N-D), the pdist2 flattens everything out to scalar distances. 这篇关于在两组矩阵之间找到最接近的点对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-11 16:33