matlab 选择最大匹配对

pdsfdshx  于 2022-12-13  发布在  Matlab
关注(0)|答案(1)|浏览(140)

我有两个不同的ID组,我得到了可能的匹配通过运行一个代码,查找的情况下,达到了标准,但是,它返回的例子,一个ID从A组,我有一个以上的匹配从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
iyfjxgzm

iyfjxgzm1#

如果我了解您的目标,我会尝试以下方法:

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

相关问题