Lion_chocolatebar
Lion_chocolatebar

Reputation: 95

Scala Breeze DenseMatrix to SparseMatrix conversion

Im struggeling to find a way to quickly convert a DenseMatrix to SparseMatrix.

I tried flattening the DenseMatrix to an array, converting it to a Sparse Matrix and then reshaping it but this is not possible since there is no reshape function..

val dm = DenseMatrix((1,2,3),(0,0,0),(0,0,0))
val sm =CSCMatrix(dm.toArray)
sm.reshape(3,3)

error: value reshape is not a member of breeze.linalg.CSCMatrix[Int]

Upvotes: 1

Views: 573

Answers (1)

Jason Scott Lenderman
Jason Scott Lenderman

Reputation: 1918

How about something like this:

val dm = DenseMatrix((1,2,3),(0,0,0),(0,0,0))
val sm = CSCMatrix.tabulate(dm.rows, dm.cols)(dm(_, _))

Upvotes: 2

Related Questions