scalascala-breeze

Breeze Vector to matrix conversion


Is there any way to convert breeze vector to breeze matrix of row/column size 1? I'm new in Scala and found it quite useful in past to write functions that handle vectors and matrices seamlessly (mostly in Matlab). For example, I would like func in following code to take as input either subsetMatrix or subsetVector.

val dummyMatrix = DenseMatrix.eye[Double](3)
val subsetMatrix = dummyMatrix(::,0 to 2)
val subsetVector = dummyMatrix(::,1)


def func(X1: DenseMatrix[Double]): Int = {
// Some operation on X1
}

Solution

  • use asDenseMatrix

    scala> import breeze.linalg._
    import breeze.linalg._
    
    scala> val dv = DenseVector(1, 2, 3)
    dv: breeze.linalg.DenseVector[Int] = DenseVector(1, 2, 3)
    
    scala> dv.asDenseMatrix
    res0: breeze.linalg.DenseMatrix[Int] = 1  2  3
    
    scala> (res0.rows, res0.cols)
    res1: (Int, Int) = (1,3)
    
    scala>