sparse matrix entries are not displayed correctly

4 views (last 30 days)
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 Comments
Walter Roberson
Walter Roberson on 28 Apr 2023
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.
Hao Hu
Hao Hu on 28 Apr 2023
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

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 28 Apr 2023
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 Comments
Matt J
Matt J on 28 Apr 2023
@Hao Hu You should Accept-click the answer to indicate that it resolved your question.
Hao Hu
Hao Hu on 28 Apr 2023
Accepted. Thanks again. It is so good to know that sparse matrix can be corrupted.

Sign in to comment.

More Answers (1)

James Tursa
James Tursa on 23 Jan 2024
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

Categories

Find more on Sparse Matrices in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!