# Function matchpairs does not work in MATLAB R2023a when I use a sparse cost matrix. Please help me out ASAP!

조회 수: 2 (최근 30일)
Ulyana Grechaniuk 2024년 4월 9일
이동: Stephen23 2024년 5월 13일
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)
matchings = 16x2
2 1 1 2 4 3 3 4 8 5 7 6 6 7 5 8 12 9 11 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
unassignedRows = 0x1 empty double column vector unassignedCols = 0x1 empty double column vector
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)
matchings = 0x2 empty double matrix
unassignedRows = 16x1
1 2 3 4 5 6 7 8 9 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
unassignedCols = 16x1
1 2 3 4 5 6 7 8 9 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

댓글을 달려면 로그인하십시오.

### 채택된 답변

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 = 16x2
2 1 1 2 4 3 3 4 8 5 7 6 6 7 5 8 12 9 11 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
unassignedRows = 0x1 empty double column vector unassignedCols = 0x1 empty double column vector
matchings
matchings = 16x2
2 1 1 2 4 3 3 4 8 5 7 6 6 7 5 8 12 9 11 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Ms = sparse(M);
[matchings, unassignedRows, unassignedCols] = matchpairs(M,12345);
matchings
matchings = 16x2
2 1 1 2 4 3 3 4 8 5 7 6 6 7 5 8 12 9 11 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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
matchings = 0x2 empty double matrix
Mf = full(Ms);
[matchings, unassignedRows, unassignedCols] = matchpairs(Mf,-1);
matchings
matchings = 0x2 empty double matrix
##### 댓글 수: 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
matchings = 16x2
2 1 1 2 4 3 3 4 8 5 7 6 6 7 5 8 12 9 11 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Ulyana Grechaniuk 2024년 4월 10일
이동: Stephen23 2024년 5월 13일
Thank you, Bruno !!!

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by