使用 x,y 坐标更新点的 scala breeze 矩阵(不是行和列的长度)

Updating scala breeze matrix for point with x,y coordinates (NOT length of rows and cols)

我是 Scala 的新手(Python 背景)并尝试为我的稀疏数据实现四叉树并尝试使用 Breeze 来实现(尽管如果您有更好的建议,我对此完全开放)。

我的问题是这样的:我需要知道如何在 (x,y) 处更新矩阵而不通过简单地递归 i <- 0 until matrix.rows 因为我没有所有行和列的值,我只有特定的 xs 和 ys。如果不清楚这就是我的意思:在 breeze 文档中,您通常会看到类似这样的内容

val r = new scala.util.Random(100)
    for(i <-0 until Matrix.rows)
        for(j <- 0 until Matrix.cols)
            Matrix(i,j) = r.nextInt
    return Matrix

如果我对矩阵中的每个值都有一个值,这很好,但我没有。相反,我正在使用的是这样的东西。

val points: Array[Double] = Array(3.0, 5.0, 8.0)
val xs: Array[Double] = Array(2.0, 5.0, 6.0)
val ys: Array[Double] = Array(3.0, 4.0, 6.0)

我想要的地方matrix(2,3) = 3.0

我知道我的矩阵应该是 6x6 矩阵 DenseMatrix[Double](6,6)

假设我从一个零矩阵 (DenseMatrix.zeros(6,6)) 开始,我如何使用我的 xsys 而不是 [=23= 插入我的 points ] 和 .cols?

我试过这个: (其中 emptym 是 6x6 零矩阵)

val matrix = for {
     | x <- xs
     | y <- ys
     | p <- points
     | } yield (emptym(x.toInt,y.toInt) = p)

这给了我各种各样的错误:/

我在想也许我可以用某种 map 函数来做到这一点,因为我想为此 return 一个 val 但我对 scala 还很陌生,所以我不太清楚了解如何做到这一点。

请帮我解决这个问题。谢谢! :)

编辑 -- 理想情况下,我希望有一个不必遍历矩阵并更新它的更多 FP 解决方案。我想创建一个新矩阵。我在想这样的事情,但无法让它工作:

val newMatrix = oldMatrix.map(xs, ys, points => oldMatirx(x,y) = point)

您可以使用update方法,在这里您可以传递行号、列号和值来更新矩阵中的单元格:

val mat = DenseMatrix.zeros[Double](6,6)
for (i <- 0 until xs.length) {
  mat.update(xs(i).toInt - 1, ys(i).toInt - 1, points(i))
}

打印出矩阵:

for (i <- 0 until mat.rows){
  for(j <- 0 until mat.cols) {
    print(mat(i, j) + " ")
  }
  println()
}

0.0 0.0 0.0 0.0 0.0 0.0 
0.0 0.0 3.0 0.0 0.0 0.0 
0.0 0.0 0.0 0.0 0.0 0.0 
0.0 0.0 0.0 0.0 0.0 0.0 
0.0 0.0 0.0 5.0 0.0 0.0 
0.0 0.0 0.0 0.0 0.0 8.0