JoePayne JoePayne - 26 days ago 10
Scala Question

How can I initialize a dense matrix with prepared matrix-like data

We can always initialize a dense matrix in breeze like this :

import breeze.linalg.{DenseMatrix => BDM}

val mt: BDM[Double] = BDM((1.0,2.0,3.0),(10.0,20.0,30.0))


Unfortunately, we cannot initialize it as follows:

import breeze.linalg.{DenseMatrix => BDM}

val data: Array[Array[Double]] = Array(Array(1.0,2.0,3.0),Array(10.0,20.0,30.0))

val mt: BDM[Double] = BDM(data)


How can I initialize a dense matrix with prepared matrix-like data like that?

Answer

To pass a collection into a function expecting a repeating parameter (aka "varargs" in Java) like DenseMatrix's constructor, use : _*:

val mt: BDM[Double] = BDM(data: _*)
Comments