Function matchpairs does not work in MATLAB R2023a when I use a sparse cost matrix. Please help me out ASAP!
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Ulyana Grechaniuk
el 9 de Abr. de 2024
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 comentarios
Respuesta aceptada
Bruno Luong
el 9 de Abr. de 2024
Editada: Bruno Luong
el 9 de Abr. de 2024
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 comentarios
Bruno Luong
el 9 de Abr. de 2024
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
Más respuestas (0)
Ver también
Categorías
Más información sobre Sparse Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!