Function matchpairs does not work in MATLAB R2023a when I use a sparse cost matrix. Please help me out ASAP!
조회 수: 2 (최근 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!