Best unique maximum matching

I have a rectangular matrix of doubles and I want to pair each column to the row with the best unique maximum, so that if two (or more) columns are paired with the same row then I pair that row with the column having the greatest maximum value among all the paired columns.
If no pairing is possible (if number of columns > number of rows) the algorithm should give back 0 or NaN.
For instance
A=[1 2 3 7;
0 3 4 6;
-1 1 5 3]
sorting it with [B,iB]=sort(A,1,'descend'):
B=
1 3 5 7
0 2 4 6
-1 1 3 3
iB=
1 2 3 1
2 1 2 2
3 3 1 3
What I would like to obtain is the column to row pairing such that:
iRowPaired = [ NaN/0 2 3 1]
Both column 1 and 4 have their maximum in row 1, but since A(1,4)>A(1,1) I pair row 1 only to column 4.
Could someone point me in the right direction for this?
Thanks.

Risposte (1)

Guillaume
Guillaume il 9 Nov 2016
This would work:
[~, iRowPaired] = max(A, [], 1); %faster than sorting
for v = unique(iRowPaired) %iterate over unique values of iRowPaired
vcols = find(v == iRowPaired); %get location of unique value
[~, maxloc] = max(A(v, vcols)); %find which is the maximum at the intersection of the row and multiple columns
vcols(maxloc) = []; %remove column of maximum from list of column
iRowPaired(vcols) = NaN; %and set the remaining columns to NaN
end

2 Commenti

Thanks Guillaume, this works. However I forgot to add before that would like to pair all the other same-paired columns to the row with the second biggest, third biggest etc. row term. If there are no more row terms available (if the matrix has n_row<n_col) then set to NaN.
Is this possible?
Thanks.
Unless I'm misunderstanding something can't you just wrap the above in a loop over the rows of iRowPaired?
[~ iRowPaired] = sort(A, 1, 'descend');
for row = 1 : size(iRowPaired, 1)
for v = unique(iRowPaired(row, :)) %iterate over unique values of current row of iRowPaired
vcols = find(v == iRowPaired(row, :)); %get locations of unique value
[~, maxloc] = max(A(v, vcols)); %find which is the maximum at the intersection of the row and multiple columns
vcols(maxloc) = []; %remove column of maximum from list of column
iRowpaired(row, vcols) = Nan; %and set the remaining columns to NaN
end
end

Accedi per commentare.

Richiesto:

il 9 Nov 2016

Commentato:

il 10 Nov 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by