简体   繁体   English

用Scala库微风填充矩阵

[英]filling a matrix with Scala library breeze

I'm new to Scala and I'm having a mental block on a seemingly easy problem. 我是Scala的新手,并且在看似简单的问题上遇到了心理障碍。 I'm using the Scala library breeze and need to take an array buffer (mutable) and put the results into a matrix. 我正在breeze使用Scala库,需要获取一个array buffer (可变)并将结果放入矩阵中。 This... should be simple but? 这...应该很简单,但是? Scala is so insanely type casted breeze seems really picky about what data types it will take when making a DenseVector. Scala是如此疯狂型铸造breeze似乎真的挑剔作出DenseVector时,它会采取什么样的数据类型。 This is just some prototype code, but can anyone help me come up with a solution? 这只是一些原型代码,但是有人可以帮助我提出解决方案吗? Right now I have something like... 现在我有类似...

//9 elements that need to go into a 3x3 matrix, 1-3 as top row, 4-6 as middle row, etc)
val numbersForMatrix: ArrayBuffer[Double] = (1, 2, 3, 4, 5, 6, 7, 8, 9) 
//the empty 3x3 matrix
var M: breeze.linalg.DenseMatrix[Double] = DenseMatrix.zeros(3,3)

In breeze you can do stuff like M(0,0) = 100 and set the first value to 100 this way, You can also do stuff like: M(0, 0 to 2) := DenseVector(1, 2, 3) which sets the first row to 1, 2, 3 But I cannot get it to do something like... 在微风中,您可以执行M(0,0) = 100并将第一个值设置为100,还可以执行以下操作: M(0, 0 to 2) := DenseVector(1, 2, 3)将第一行设置为1、2、3,但是我无法执行类似...的操作

var dummyList: List[Double] = List(1, 2, 3) //this works
var dummyVec = DenseVector[Double](dummyList) //this works
M(0, 0 to 2) := dummyVec //this does not work

and successfully change the first row to the 1, 2,3. 并将第一行成功更改为1,2,3。 And that's with a List, not even an ArrayBuffer. 那是一个列表,甚至没有一个ArrayBuffer。

Am willing to change datatypes from ArrayBuffer but just not sure how to approach this at all... could try updating the matrix values one by one but that seems like it would be VERY hacky to code up(?). 我愿意从ArrayBuffer更改数据类型,但只是根本不确定该如何处理...可以尝试一一更新矩阵值,但是编码起来似乎很不方便(?)。

Note: I'm a Python programmer who is used to using numpy and just giving it arrays. 注意:我是一位Python程序员,习惯于使用numpy并只给它提供数组。 The breeze documentation doesn't provide enough examples with other datatypes for me to have been able to figure this out yet. breeze文档还没有提供足够的其他数据类型示例,因此我无法弄清这一点。 Thanks! 谢谢!

Breeze除了对类型进行挑剔之外,还对矢量形状非常挑剔:DenseVectors是列矢量,但是您尝试分配给一行的子集,该子集需要转置的DenseVector:

M(0, 0 to 2) := dummyVec.t

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

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