Home > Blockchain >  matlab spare matrix to python
matlab spare matrix to python

Time:08-19

I want to get a sparse matrix of dimension 16995 by 16995 in python. I have the syntax in matlab and I am not sure how can I write in python to get the same syntax as well as the output like matlab.

The matlab synttax :

  C = [s1 s2 s3;s4 s5 s6;s7 s8 s9];

where s1,s2,s3,s4,s5,s6,s7,s8,s9 is a sparse csr matrix each of dimension 5665 by 5665 . How can I write the same syntax in python to get the exact dimension and a sparse matrix.

I tried using block but it does not give me the right dimension. As requested the matlab version of the code :

CodePudding user response:

This is best achieved through numpy.hstack and numpy.vstack.

import numpy as np

C = np.vstack([
  np.hstack([s1, s2, s3]),
  np.hstack([s4, s5, s6]),
  np.hstack([s7, s8, s9]),
])

CodePudding user response:

Solution :

  C = vstack([
  hstack([s1, s2, s3]),
  hstack([s4, s5, s6]),
  hstack([s7, s8, s9])
  ],format='csr')
  • Related