简体   繁体   English

如何在Scala中将序列变量转换为矩阵

[英]How to convert a sequence variable into a matrix in Scala

This should be easy, but I'm stuck. 这应该很容易,但是我被卡住了。 I'm trying to convert a Seq[Seq[Double]] type into a Breeze DenseMatrix . 我正在尝试将Seq[Seq[Double]]类型转换为Breeze DenseMatrix (all nested Seq[Double]'s have the same number of elements.) (所有嵌套的Seq [Double]具有相同数量的元素。)

Converting a single sequence to a DenseVector is pretty easy: 将单个序列转换为DenseVector非常简单:

val sss=Seq(2.3,3.4,2.0,1.0)
val bbb=DenseVector(sss:_*)

Is there any similar approach to convert a Seq[Seq[Double]] type to a DenseMatrix ? 是否有类似的方法将Seq[Seq[Double]]类型转换为DenseMatrix For example: 例如:

val sss=Seq(2.3,3.4,2.0,1.0)
val sssM=Seq(sss,sss,sss)
val bbb=DenseVector(sss:_*)
//val bbm= DenseMatrix(sssM:_*:??)  //????

I noticed that the same approach works fine: 我注意到相同的方法可以正常工作:

val sss=Seq(2.3,3.4,2.0,1.0)
val sssM=Seq(sss,sss,sss)
val bbb=DenseVector(sss:_*)
val bbm= DenseMatrix(sssM:_*)

Initially I had thought that we need to expand each nested sequence as well. 最初,我以为我们也需要扩展每个嵌套序列。 But as stated by Rex Kerr, in a comment below, the Breeze library automatically takes care of that. 但是正如Rex Kerr所述,在下面的评论中,Breeze库自动解决了这一问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM