繁体   English   中英

选择最大匹配对

[英]Selecting the maximum matched pairs

我有两个具有不同 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM