简体   繁体   中英

Selecting the maximum matched pairs

I have two groups with different IDs, I got the possible matches by running a code that looked into cases that achieved the criteria, however, it returned for example for one ID from Group A, I have more than one match from Group B. I would like to get rid of the repetition and choose the matched pair randomly that achieved the maximum number of matched pairs at the end. Any Idea of how to solve this?

Here is my code:

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

If I understand your objective I would try the following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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