0

I have one very large tall sparse matrix1 (MxN , where M >>>> N)

I want to assign chunks of it to two other very large sparse matrices of size matrix2: MxN1 and matrix3: MxN2 (N1 + N2 != N)

What I am doing right now is

matrix2(1:rowIndex1, 1:colIndex1) = matrix1(1:rowIndex1, 1:colIndex1)
matrix3(1:rowIndex1, 1:colIndex2) = matrix2(1:rowIndex1, colIndex1+1:colIndex1+someK)

This operation is taking forever. Is there a faster way to do this?

Rahul
  • 3,220
  • 4
  • 22
  • 28
  • Are the resulting matrices again sparse? Have you tried pre-allocating using `zeros` or `sparse`? – Matthias W. Feb 03 '16 at 17:00
  • Do `matrix2` and `matrix3` exist beforehand? – Ander Biguri Feb 03 '16 at 17:00
  • Yes they are sparse and preallocated, I initialize them with sparse(size) – Rahul Feb 03 '16 at 17:00
  • I can not try it now but my guess is that that matrix indexing is very slow. Probably its easier to get the non zero entry indexes, crop then using `rowindex` and `colindex` and access the matrix using only them – Ander Biguri Feb 03 '16 at 17:09
  • 3
    Assigning elements to sparse matrices after you create them is notoriously slow. It requires a lot of processing in order to do this. Your best bet is to initialize the `sparse` matrix with these non-zero entries instead of creating an empty sparse matrix and assigning after the fact. Consult the duplicate I have linked above. – rayryeng Feb 03 '16 at 17:09
  • This is happening in a loop over a small number. So I cant initialize them just once. But I see that this operation cannot work as is. Thanks for the information. I will consider indexing them as @AnderBiguri suggested – Rahul Feb 03 '16 at 17:15
  • 2
    @Rahul create vectors of row and column locations and their associated non-zero entries then when the vectors are finally created, initialize the `sparse` matrix. It's faster to do it this way than to create a `sparse` matrix then do the assignment. It's a general rule to **never** do this. `sparse` matrices are compact representations of matrices with very few nonzero terms and they're great when solving linear systems of equations, but actually trying to assign elements to them beyond when they're initialized is never a good course of action. – rayryeng Feb 03 '16 at 17:19
  • 1
    Got it! Thanks for the help @rayryeng – Rahul Feb 03 '16 at 17:22
  • @Rahul you're very welcome. Good luck! – rayryeng Feb 03 '16 at 17:24

0 Answers0