假設要整體差異的A
元件和匹配元件之間的最小化在B
,問題可被寫爲分配給每行(的A
元件)的柱(的B
元件)的分配問題給出了一個成本矩陣C
。匈牙利語(或Munkres')算法解決了分配問題。
我假設你希望儘量減少在B
A
之間匹配元素累積平方距離,並通過異草從https://www.mathworks.com/matlabcentral/fileexchange/20652-hungarian-algorithm-for-linear-assignment-problems--v2-3-使用功能[assignment,cost] = munkres(costMat)
:
A = [1 5 7];
B = [1 2 3 6 9 10];
[Bprime,matches] = matching(A,B)
function [Bprime,matches] = matching(A,B)
C = (repmat(A',1,length(B)) - repmat(B,length(A),1)).^2;
[matches,~] = munkres(C);
Bprime = B(matches);
end
假設,而不是你想遞歸尋找匹配 ,正如你的問題所建議的那樣,你可以通過A
,A
中的每個元素找到B
中最接近的剩餘元素並丟棄它(下面的sortedmatching
);潛在
A = [1 5 7];
B = [1 2 3 6 9 10];
[~,~,Bprime,matches] = sortedmatching(A,B,[],[])
[~,~,Bprime,matches] = greedymatching(A,B,[],[])
function [A,B,Bprime,matches] = sortedmatching(A,B,Bprime,matches)
[~,ix] = min((A(1) - B).^2);
matches = [matches ix];
Bprime = [Bprime B(ix)];
A = A(2:end);
B(ix) = Inf;
if(not(isempty(A)))
[A,B,Bprime,matches] = sortedmatching(A,B,Bprime,matches);
end
end
function [A,B,Bprime,matches] = greedymatching(A,B,Bprime,matches)
C = (repmat(A',1,length(B)) - repmat(B,length(A),1)).^2;
[minrows,ixrows] = min(C);
[~,ixcol] = min(minrows);
ixrow = ixrows(ixcol);
matches(ixrow) = ixcol;
Bprime(ixrow) = B(ixcol);
A(ixrow) = -Inf;
B(ixcol) = Inf;
if(max(A) > -Inf)
[A,B,Bprime,matches] = greedymatching(A,B,Bprime,matches);
end
end
同時產生在你的例子相同的結果,所有的三種方法:或者你可以迭代地形成,並丟棄剩餘的元件之間的距離最小化的匹配在A
和B
直到A
所有元素都匹配(greedymatching
)對相同的數據給出不同的答案。
你可以用'interp1'結果。可能的重複http://stackoverflow.com/a/40809851/6579744 – rahnema1