选择最大匹配对



我有两个具有不同ID的组,我通过运行查看达到标准的情况的代码来获得可能的匹配,但是,它返回例如来自a组的一个ID,我有多个来自b组的匹配。我想摆脱重复并随机选择匹配的对,在结束时达到最大匹配对的数量。知道怎么解吗?

下面是我的代码:
SH = readtable('contol_parameters.xlsx','Sheet','m');
%% check if crieria met 
numElementsX = length(rmmissing(SH.Ages1));
numElementsY = length(rmmissing(SH.Ages2));
U1 = [];
U2=  [];
for r=1:numElementsX
for s=1:numElementsY
if (abs(rmmissing(SH.Ages1(r))-rmmissing(SH.Ages2(s)))<=10) && (abs(rmmissing(SH.vol_1(r))-rmmissing(SH.vol_2(s)))<=10)
U1(end+1)= SH.ID1(r);
U2(end+1)= SH.ID2(s);
end
end
end
%generated list 
U_TS=[U1', U2'];
%results 
Group A Group B
216 217
216 221
216 222
216 234
216 256
216 262
216 266
216 330
216 390
225 217
225 222
225 234
225 239
225 256
225 257
225 260
225 263
225 266
225 277
225 302
225 324
225 330
225 333
225 341
225 359
225 381
225 386
225 390
225 423
225 435
225 436
225 442
225 466
225 470
225 478
227 257
227 260
227 263
227 277
227 302

如果我明白你的目标,我会尝试以下方法:

uA = unique(A);
uB = unique(B);
iCnt = zeros(length(uA),length(uB);
for ii = 1:length(uA)
for jj = 1:length(uB)
iCnt(ii,jj) = sum((A==uA(ii) & B==uB(jj));
end
end
[~,ind] = sort(sum(iCnt),'ascend');
uB = uB(ind);
iCnt = iCnt(:,ind);
%you now have a matrix (iCnt) where the least common members of groupB will be in the leftmost columns of iCnt and for each row (which represents the unique members of GroupA in vector uA) you can find the first non-zero column of iCnt to pick the least common member of GroupB. If that member of B has already been selected previously, you could go to the next non-zero column for another candidate

相关内容

  • 没有找到相关文章

最新更新