sparse matrix entries are not displayed correctly

A sparse matrix variable has a very strange behavior.
A minimal example is given as follows: this is a 2 by 4 matrix, and I display them in both full and sparse ways for clarification
>> full(A)
ans =
0 0 0 198844
0 0 0 23672
>> A
A =
(2,4) 23672
(1,4) 198844
The problem is: if we print the first row, then it shows tht the first row contains zeros, which is not true.
>> A(1,:)
ans =
All zero sparse: 1×4
However, the second rows are printed correctly, and if we print both first&second rows and fouth column, it is also correct.
>> A(2,:)
ans =
(1,4) 23672
>> A(1:2,4)
ans =
(2,1) 23672
(1,1) 198844
remarks:
  1. These commands are entered without doing anything in between.
  2. A(1,4) is also considered as zero if I make any calculations, for example, A(1,4)*5 gives zero.

댓글 수: 2

A = sparse([ 0 0 0 198844
0 0 0 23672])
A =
(1,4) 198844 (2,4) 23672
A(1,:)
ans =
(1,4) 198844
A(2,:)
ans =
(1,4) 23672
Could you save the matrix to a .mat file and attach the mat file for testing?
Also, please fill out which Release you are using.
Thanks for your response. I have attached the .mat file containg the minimal example, and the release is "MATLAB Version: 9.11.0.1809720 (R2021b) Update 1"
After loading the mat file, we can see the bug by typing
>> load('example_sp_A.mat')
>> A
A =
(2,4) 23672
(1,4) 198844
>> A(1,4)
ans =
All zero sparse: 1×1

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

 채택된 답변

Notice that the matrix is displayed out of order -- the normal order is linear indexing, so (1,4) would have appeared before (2,4)
I suspect that the matrix was created by third-party software that (somehow) did not follow internal rules about constructing sparse matrices.
load('example_sp_A.mat')
A
A =
(2,4) 23672 (1,4) 198844
B = sparse(full(A))
B =
(1,4) 198844 (2,4) 23672
A(1,:)
ans =
All zero sparse: 1×4
B(1,:)
ans =
(1,4) 198844
mask = A ~= B
mask = 2×4 sparse logical array
(1,4) 1 (1,4) 1
full(mask)
ans = 2×4 logical array
0 0 0 1 0 0 0 0
nnz(mask)
ans = 2
So we can construct a "repaired" version of the matrix by taking it full, but in the meantime working with A gets us messed up results, such as a sparse array that has two elements both at the same location.
In summary, I think A was constructed corrupted, either by a third party routine writing a corrupted .mat or else by a malfunctioning mex routine.

댓글 수: 4

Matt J
Matt J 2023년 4월 28일
편집: Matt J 2023년 4월 28일
Using TIm Davis's spok shows that it is indeed corrupted:
>> spok(A)
Warning: 1 jumbled row indices, 0 explicit zeros
ans =
0
It is indeed produced by a third party routine. More specifically, it is the a subroutine called "Gurobi_read()" from gurobi. It is used to read a mathematical programming file in .mps format. In this case, the problem 'app2-2.mps' is being read. The trick "convert to full and back to sparse" works very well. Hopefully this information is useful for anyone who encounter the same problem.
Thanks, Walter. This helps a lot.
@Hao Hu You should Accept-click the answer to indicate that it resolved your question.
Accepted. Thanks again. It is so good to know that sparse matrix can be corrupted.

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

추가 답변 (1개)

James Tursa
James Tursa 2024년 1월 23일
I finally finished my sparse matrix integrity checker. You can find it in the FEX under the name SAREK (I think you will be able to figure out the inside joke):
Here is what is reports for the posted matrix:
>> sarek(A)
SAREK -- Sparse Analyzer Real Et Komplex , by James Tursa
Compiled in version R2023a (with -R2018a option)
Running in version R2023a
Matrix is double ...
Matrix is real ...
M = 2 >= 0 OK ...
N = 4 >= 0 OK ...
Nzmax = 2 >= 1 OK ...
Jc[0] == 0 OK ...
Jc[N] = 2 <= Nzmax = 2 OK ...
Jc array OK ...
ERROR: There are 1 Ir entries out of order or out of range
TO FIX: [M,N] = size(A); [I,J,V] = find(A); B = sparse(I,J,V,max(max(I),M),N);
All stored elements nonzero OK ...
There were ERRORS found in matrix!
ans =
1

카테고리

도움말 센터File Exchange에서 Sparse Matrices에 대해 자세히 알아보기

제품

릴리스

R2021b

질문:

2023년 4월 28일

답변:

2024년 1월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by