Why is indexing a sparse matrix not behaving how I would expect?

3 vues (au cours des 30 derniers jours)
Alexander Audet
Alexander Audet le 24 Juin 2024
Modifié(e) : John D'Errico le 25 Juin 2024
I am trying to understand someone else's code so I don't entirely understand what is happening (it is trying to fill in missing values) but this is what I am observing. I have the following sparse matrix:
val =
(1,41) 1
(1,42) -2
(2,42) 1
(1,43) 1
(2,43) -2
(3,43) 1
(2,44) 1
(3,44) -2
(3,45) 1
(4,45) 1
(4,46) -2
(5,46) 1
(4,47) 1
(5,47) -2
(6,47) 1
(5,48) 1
(6,48) -2
(6,49) 1
(7,53) 1
(7,54) -2
(8,54) 1
(7,55) 1
(8,55) -2
(9,55) 1
(8,56) 1
(9,56) -2
(10,56) 1
(9,57) 1
(10,57) -2
(10,58) 1
I also have a kown list of indexes that is almost continuous from 1:100 but excludes index numbers 43, 47, 55, 56.
When I try to index
sparse(:,known_list);
I would expect to get a list excluding all the unknown values 43, 47, 55, 56. So that I would have something like this:
val =
(1,41) 1
(1,42) -2
(2,42) 1
(2,44) 1
(3,44) -2
(3,45) 1
(4,45) 1
(4,46) -2
(5,46) 1
(5,48) 1
(6,48) -2
(6,49) 1
(7,53) 1
(7,54) -2
(8,54) 1
(9,57) 1
(10,57) -2
(10,58) 1
But instead I get:
ans =
(1,41) 1
(1,42) -2
(2,42) 1
(2,43) 1
(3,43) -2
(3,44) 1
(4,44) 1
(4,45) -2
(5,45) 1
(5,46) 1
(6,46) -2
(6,47) 1
(7,51) 1
(7,52) -2
(8,52) 1
(9,53) 1
(10,53) -2
(10,54) 1
I can't make head or tails of the logic Matlab is using!!! I thought I understood sparse matrixes, but maybe there is something I am not getting!?!?

Réponses (1)

John D'Errico
John D'Errico le 24 Juin 2024
Modifié(e) : John D'Errico le 25 Juin 2024
This is not a question about sparse matrices. Well, it is, but it is not really. Sparse matrices are exactly the same as any other matrix. So if you understand how to use matrix indexing, then you understand sparse matrix indexing. I think howaever, you are confused, and I see where that confusion arises. Let me give you an example, since I do not have your original matrix.
A = sprand(10,10,0.1)
A =
(3,1) 0.1393 (4,1) 0.1035 (10,1) 0.7283 (1,2) 0.3255 (8,3) 0.3391 (9,4) 0.0374 (3,6) 0.6882 (4,7) 0.0703 (8,9) 0.6428
So a 10x10 sparse matrix, 10% non-zero, roughly. Now, let me index into that matrix. I will choose only the odd numbered columns.
B = A(:,1:2:end)
B =
(3,1) 0.1393 (4,1) 0.1035 (10,1) 0.7283 (8,2) 0.3391 (4,4) 0.0703 (8,5) 0.6428
Now, do you see that the result shows columns with both EVEN and ODD numbers????? How did that happen? What is the size of the result? It is again a sparse matrix. But the SECOND column of the result matrix, B, is the third column of the original matrix A. We can see better what is happening if you look at the matrices in full form.
full(A)
ans = 10x10
0 0.3255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.1393 0 0 0 0 0.6882 0 0 0 0 0.1035 0 0 0 0 0 0.0703 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.3391 0 0 0 0 0 0.6428 0 0 0 0 0.0374 0 0 0 0 0 0 0.7283 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
full(B)
ans = 10x5
0 0 0 0 0 0 0 0 0 0 0.1393 0 0 0 0 0.1035 0 0 0.0703 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.3391 0 0 0.6428 0 0 0 0 0 0.7283 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Do you see that the result matrix B has columns with both even and odd numbered columns? I think you are confused, because the matrix B is itself a sparse matrix.
But in your case, of course the new matrix, AFTER indexing will have columns numbered 43. The column numbers of B do NOT correspond to the column numbers of matrix A.
Would that have been a source of confusion if the matrix was not sparse? Well, no. It is only because of the way the sparse matrix is displayed, where it shows the row and column numbers of each non-zero element. If the matrices were full, you would not have seen those column numbers, and so you would not have been confused. Again though, this is just basic matrix indexing. The result is on its own as a completely new matrix.
Consider doing the same thing, if the matrix was full.
A = magic(5)
A = 5x5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
B = A(:,1:2:end)
B = 5x3
17 1 15 23 7 16 4 13 22 10 19 3 11 25 9
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
You should see and I think, fully understand the matrix B has a SECOND column. It was originally the third column of matrix A. But when you look at the matrix B, B has both even and odd numbered columns. This would not surprise you, I think. So why is it a problem when you look at a sparse matix? You don't care about the original numbering with respect to the matrix A.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by