官术网_书友最值得收藏!

Types of matrices

In Scala, we will use the Breeze library to represent a matrix. A matrix can be represented as a dense or a CSC matrix.

  • Dense matrix: A dense matrix is created with a constructor method call. Its elements can be accessed and updated. It is a column major, and can be transposed to convert to row major.
        val a = DenseMatrix((1,2),(3,4)) 
println("a : n" + a)
val m = DenseMatrix.zeros[Int](5,5)

The columns of a matrix can be accessed as Dense Vectors, and
the rows as Dense Matrices.

println( "m.rows :" + m.rows + " m.cols : " + m.cols)
m(::,1)
println("m : n" + m)
  • Transposing a matrix: Transposing a matrix means swapping its rows and columns. The transpose of a P × Q matrix, written MT, is a Q × P matrix such that (MT ) j, I = Mi, j for every I ∈ P, j ∈ Q. Vector transpose to create a matrix row.
        m(4,::) := DenseVector(5,5,5,5,5).t 
println(m)
The output of the preceding program is as follows:
      
a :
1 2
3 4
Created a 5x5 matrix
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
m.rows :5 m.cols : 5
First Column of m :
DenseVector(0, 0, 0, 0, 0)
Assigned 5,5,5,5,5 to last row of m.

0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
5 5 5 5 5
  • CSC matrix: The CSC matrix is known as the Compressed Sparse Columns matrix. Each column within the CSC matrix represents a sparse vector. The CSC matrix supports all matrix operations, and is constructed using Builder.
        val builder = new CSCMatrix.Builder[Double](rows=10, 
cols=10)
builder.add(3,4, 1.0)
// etc.
val myMatrix = builder.result()
主站蜘蛛池模板: 义马市| 泰和县| 桂阳县| 库伦旗| 樟树市| 卢氏县| 新干县| 栖霞市| 洪湖市| 老河口市| 清徐县| 清苑县| 隆尧县| 阿尔山市| 洪江市| 长宁区| 香河县| 石屏县| 墨竹工卡县| 天长市| 天台县| 拜泉县| 日土县| 芦溪县| 高雄市| 南雄市| 河北省| 鄂托克旗| 拜城县| 濮阳市| 沈阳市| 大英县| 滦南县| 新蔡县| 冕宁县| 洞头县| 拉孜县| 滦平县| 闵行区| 宝应县| 佛山市|