Why is indexing a sparse matrix not behaving how I would expect?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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!?!?
0 comentarios
Respuestas (1)
John D'Errico
el 24 de Jun. de 2024
Editada: John D'Errico
el 25 de Jun. de 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)
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)
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)
full(B)
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)
B = A(:,1:2:end)
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.
0 comentarios
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!