I want to create a matrix where the element number (i, j) that is not 0 of the large sparse matrix is ​​the difference between the i-th and j-th vectors.

# A is a sparse matrix
#the dimension of A is 1000000 × 1000000
rows, cols = A.nonzero ()
index = [(i, j) for i, j in zip (rows, cols)] Set of non-zero element numbers
print (len (index))
# 5000000
B = A.copy ()
#y is a vector
#the dimension of y is 1000000
for i, j in index:
    B [i, j] = y [i]-y [j]

You can actually create a matrix.
However, as shown above, a large matrix is ​​used. Therefore, it takes a long time to execute.
I think this is because the execution speed of the for statement is slow in python.

Tell me if there is a way to do it faster.

  • Answer # 1

    import scipy.sparse
    rows, cols = A.nonzero ()
    data = y [rows] -y [cols]
    B = sparse.coo_matrix ((data, (rows, cols)))

Related articles