有没有办法将breeze矢量转换为行/列大小为1的breeze矩阵?我是Scala的新手,发现在过去编写无缝处理向量和矩阵的函数(主要是在Matlab中)非常有用。例如,我想在下面的代码中使用func作为subsetMatrix或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
}
答案 0 :(得分:5)
使用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>
答案 1 :(得分:0)
更好的解决方案通常是首先创建subsetVector
时使用范围切片(如果您希望它是Nx1矩阵类型而不是length-N向量类型)。例如:
val subsetSingleColumnMatrix = dummyMatrix(::, 0 until 1)
例如:
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