如何在 Scala 中定义和初始化矩阵

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25093483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 06:28:20  来源:igfitidea点击:

How to define & init Matrix in Scala

arraysscalamatrix

提问by Alex L

I have a class which has two dimensional array as a private member - k rows and n columns (the size is not known when defining the Matrix).

我有一个类,它具有二维数组作为私有成员 - k 行和 n 列(定义矩阵时不知道大小)。

I want init the Matrix using a special method: initMatrix, which will set the number of rows and cols in the matrix, and init all the data to 0.

我想使用一种特殊的方法初始化矩阵:initMatrix,它将设置矩阵中的行数和列数,并将所有数据初始化为 0。

I saw a way to init a multi-dimensional array in the following way:

我看到了一种通过以下方式初始化多维数组的方法:

  private var relationMatrix = Array.ofDim[Float](numOfRows,numOfCols)

but how can I define it without any size, and init it later?

但是我如何定义它没有任何大小,并在以后初始化它?

采纳答案by Radian

Did you consider using an Option?

你考虑过使用 Option 吗?

class MyClass() {
  private var relationMatrix: Option[Array[Array[Float]]] = None

  def initMatrix(numOfRows:Int, numOfCols:Int): Unit = {
    relationMatrix = Some(/* Your initialization code here */)
  }
}

The pros of this approach is that in any time you can know whether your matrix is initialized or not, by using relationMatrix.isDefined, or by doing pattern matching,

这种方法的优点是,您可以通过使用relationMatrix.isDefined或进行模式匹配,随时知道您的矩阵是否已初始化,

def matrixOperation: Float = relationMatrix match { 
  case Some(matrix) =>
    // Matrix is initialized
  case None =>
    0 // Matrix is not initialized
}

or map on it, like this:

或在其上映射,如下所示:

def matrixOperation: Option[Float] = relationMatrix.map { 
  matrix: Array[Array[Float]] =>
  // Operation logic here, executes only if the matrix is initialized 
}

回答by elm

For declaring a mutable 2D array of floats without setting dimensions and values, consider

要在不设置维度和值的情况下声明可变的二维浮点数组,请考虑

var a: Array[Array[Float]] = _
a: Array[Array[Float]] = null

For initialising it consider the use of Array.tabulatelike this

为了初始化它考虑使用Array.tabulate这样的

def init (nRows: Int, nCols: Int) = Array.tabulate(nRows,nCols)( (x,y) => 0f )

Thus for instance,

因此,例如,

a = init(2,3)

delivers

提供

Array[Array[Float]] = Array(Array(0.0, 0.0, 0.0), Array(0.0, 0.0, 0.0))

Updatethe use of Option(already suggested by @Radian) proves type-safe and more robust at run-time, in contrast with nullinitialisation; hence, consider also Option(Array.tabulate).

Optionnull初始化相比,更新使用(已由@Radian 建议)证明类型安全且在运行时更健壮;因此,还要考虑Option(Array.tabulate)

回答by Ashalynd

You can make your initMatrix method part of the companion object.

您可以使您的 initMatrix 方法成为伴随对象的一部分。

class Matrix(numOfRows:Int, numOfCols:Int) {
    private var relationMatrix = Array.ofDim[Float](numOfRows, numOfCols)
// your code there

}

object Matrix {
    def initMatrix(numOfRows:Int, numOfCols:Int) = Matrix(numOfRows, numOfCols)
}

var myMatrix = Matrix.initMatrix(3,5)

回答by Display Name

var matrix: Array[Array[Float]] = null— this declares a variable of the same type as yours

var matrix: Array[Array[Float]] = null— 这声明了一个与你的类型相同的变量