Function matchpairs does not work in MATLAB R2023a when I use a sparse cost matrix. Please help me out ASAP!
3 次查看(过去 30 天)
显示 更早的评论
You can run this simple code and see my problem. Am I doing something wrong?
% testing matchpairs ----------------------------------------------------------------
clear;clc
% --- using matrix in usual form (not sparse) --- WORKS
M = 99*ones(16,16);
M(1,1) = 2;
M(2,1) = 1;
M(1,2) = 1;
M(3,2) = 2;
M(2,3) = 2;
M(4,3) = 1;
M(6,3) = 4;
M(3,4) = 1;
M(4,4) = 2;
M(7,4) = 4;
M(6,5) = 2;
M(8,5) = 1;
M(3,6) = 4;
M(5,6) = 2;
M(7,6) = 1;
M(4,7) = 4;
M(6,7) = 1;
M(10,7) = 2;
M(5,8) = 1;
M(9,8) = 2;
M(8,9) = 2;
M(12,9) = 1;
M(7,10) = 2;
M(11,10) = 1;
M(13,10) = 4;
M(10,11) = 1;
M(12,11) = 2;
M(14,11) = 4;
M(9,12) = 1;
M(11,12) = 2;
M(10,13) = 4;
M(13,13) = 2;
M(14,13) = 1;
M(11,14) = 4;
M(13,14) = 1;
M(15,14) = 2;
M(14,15) = 2;
M(16,15) = 1;
M(15,16) = 1;
M(16,16) = 2;
[matchings, unassignedRows, unassignedCols] = matchpairs(M,12345)
OUTPUT IS CORRECT:
% --- using sparse matrix --- DOES NOT WORK
Ms = sparse(16,16);
Ms(1,1) = 2;
Ms(2,1) = 1;
Ms(1,2) = 1;
Ms(3,2) = 2;
Ms(2,3) = 2;
Ms(4,3) = 1;
Ms(6,3) = 4;
Ms(3,4) = 1;
Ms(4,4) = 2;
Ms(7,4) = 4;
Ms(6,5) = 2;
Ms(8,5) = 1;
Ms(3,6) = 4;
Ms(5,6) = 2;
Ms(7,6) = 1;
Ms(4,7) = 4;
Ms(6,7) = 1;
Ms(10,7) = 2;
Ms(5,8) = 1;
Ms(9,8) = 2;
Ms(8,9) = 2;
Ms(12,9) = 1;
Ms(7,10) = 2;
Ms(11,10) = 1;
Ms(13,10) = 4;
Ms(10,11) = 1;
Ms(12,11) = 2;
Ms(14,11) = 4;
Ms(9,12) = 1;
Ms(11,12) = 2;
Ms(10,13) = 4;
Ms(13,13) = 2;
Ms(14,13) = 1;
Ms(11,14) = 4;
Ms(13,14) = 1;
Ms(15,14) = 2;
Ms(14,15) = 2;
Ms(16,15) = 1;
Ms(15,16) = 1;
Ms(16,16) = 2;
[matchings, unassignedRows, unassignedCols] = matchpairs(Ms,-1)
0 个评论
采纳的回答
Bruno Luong
2024-4-9
编辑:Bruno Luong
2024-4-9
Your sparse matrix filled default cost wit 0 not 99. Then your expectation on that the result is identocal for different cost matrices (regardless data structure using) is simply plain wrong.
If you fill sparse or dense matrix with the same values it give identical outputs:
% testing matchpairs ----------------------------------------------------------------
clear;clc
% --- using matrix in usual form (not sparse) --- WORKS
M = 99*ones(16,16);
M(1,1) = 2;
M(2,1) = 1;
M(1,2) = 1;
M(3,2) = 2;
M(2,3) = 2;
M(4,3) = 1;
M(6,3) = 4;
M(3,4) = 1;
M(4,4) = 2;
M(7,4) = 4;
M(6,5) = 2;
M(8,5) = 1;
M(3,6) = 4;
M(5,6) = 2;
M(7,6) = 1;
M(4,7) = 4;
M(6,7) = 1;
M(10,7) = 2;
M(5,8) = 1;
M(9,8) = 2;
M(8,9) = 2;
M(12,9) = 1;
M(7,10) = 2;
M(11,10) = 1;
M(13,10) = 4;
M(10,11) = 1;
M(12,11) = 2;
M(14,11) = 4;
M(9,12) = 1;
M(11,12) = 2;
M(10,13) = 4;
M(13,13) = 2;
M(14,13) = 1;
M(11,14) = 4;
M(13,14) = 1;
M(15,14) = 2;
M(14,15) = 2;
M(16,15) = 1;
M(15,16) = 1;
M(16,16) = 2;
[matchings, unassignedRows, unassignedCols] = matchpairs(M,12345)
matchings
Ms = sparse(M);
[matchings, unassignedRows, unassignedCols] = matchpairs(M,12345);
matchings
Cross check, now both filled with 0s
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% --- using sparse matrix --- DOES NOT WORK
Ms = sparse(16,16);
Ms(1,1) = 2;
Ms(2,1) = 1;
Ms(1,2) = 1;
Ms(3,2) = 2;
Ms(2,3) = 2;
Ms(4,3) = 1;
Ms(6,3) = 4;
Ms(3,4) = 1;
Ms(4,4) = 2;
Ms(7,4) = 4;
Ms(6,5) = 2;
Ms(8,5) = 1;
Ms(3,6) = 4;
Ms(5,6) = 2;
Ms(7,6) = 1;
Ms(4,7) = 4;
Ms(6,7) = 1;
Ms(10,7) = 2;
Ms(5,8) = 1;
Ms(9,8) = 2;
Ms(8,9) = 2;
Ms(12,9) = 1;
Ms(7,10) = 2;
Ms(11,10) = 1;
Ms(13,10) = 4;
Ms(10,11) = 1;
Ms(12,11) = 2;
Ms(14,11) = 4;
Ms(9,12) = 1;
Ms(11,12) = 2;
Ms(10,13) = 4;
Ms(13,13) = 2;
Ms(14,13) = 1;
Ms(11,14) = 4;
Ms(13,14) = 1;
Ms(15,14) = 2;
Ms(14,15) = 2;
Ms(16,15) = 1;
Ms(15,16) = 1;
Ms(16,16) = 2;
[matchings, unassignedRows, unassignedCols] = matchpairs(Ms,-1);
matchings
Mf = full(Ms);
[matchings, unassignedRows, unassignedCols] = matchpairs(Mf,-1);
matchings
2 个评论
Bruno Luong
2024-4-9
One way to use sparse and keep filling 0 is to shift the value of the cost matrix
% testing matchpairs ----------------------------------------------------------------
Ms = sparse(16,16);
Ms(1,1) = 2;
Ms(2,1) = 1;
Ms(1,2) = 1;
Ms(3,2) = 2;
Ms(2,3) = 2;
Ms(4,3) = 1;
Ms(6,3) = 4;
Ms(3,4) = 1;
Ms(4,4) = 2;
Ms(7,4) = 4;
Ms(6,5) = 2;
Ms(8,5) = 1;
Ms(3,6) = 4;
Ms(5,6) = 2;
Ms(7,6) = 1;
Ms(4,7) = 4;
Ms(6,7) = 1;
Ms(10,7) = 2;
Ms(5,8) = 1;
Ms(9,8) = 2;
Ms(8,9) = 2;
Ms(12,9) = 1;
Ms(7,10) = 2;
Ms(11,10) = 1;
Ms(13,10) = 4;
Ms(10,11) = 1;
Ms(12,11) = 2;
Ms(14,11) = 4;
Ms(9,12) = 1;
Ms(11,12) = 2;
Ms(10,13) = 4;
Ms(13,13) = 2;
Ms(14,13) = 1;
Ms(11,14) = 4;
Ms(13,14) = 1;
Ms(15,14) = 2;
Ms(14,15) = 2;
Ms(16,15) = 1;
Ms(15,16) = 1;
Ms(16,16) = 2;
% Shift matrix
[I,J,K] = find(Ms);
fillvalue = 99;
Msshift = sparse(I,J,K-fillvalue,16,16);
[matchings, unassignedRows, unassignedCols] = matchpairs(Msshift,12345);
matchings
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!