Reputation: 103
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
}
Upvotes: 2
Views: 3332
Reputation: 77424
A better solution is often to use range slicing when creating subsetVector
in the first place (if you want it to be an Nx1 matrix-type instead of length-N vector type). For example:
val subsetSingleColumnMatrix = dummyMatrix(::, 0 until 1)
such as:
scala> val z = DenseMatrix.rand[Double](3, 3)
z: breeze.linalg.DenseMatrix[Double] =
0.9523399908830603 0.5140714248369589 0.23363266806760596
0.27656335841627455 0.2143774018347031 0.3116275714282011
0.020435608706944386 0.13954770594758292 0.5493312961226657
scala> z(::, 0 until 1)
res47: breeze.linalg.DenseMatrix[Double] =
0.9523399908830603
0.27656335841627455
0.020435608706944386
Upvotes: 1
Reputation: 2293
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>
Upvotes: 5