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

Matrix operations

There are different kinds of operations which can be performed on matrices.

  • Elementwise addition: Given two matrices, a and b, addition of the two (a + b) means adding each element of two matrices.

               Breeze

        val a = DenseMatrix((1,2),(3,4)) 
val b = DenseMatrix((2,2),(2,2))
val c = a + b
println("a: n" + a)
println("b: n" + b)
println("a + b : n" + c)

The output of the last code is given as follows:

      a: 
1 2
3 4
b:
2 2
2 2
a + b :
3 4
5 6
  • Elementwise multiplication: In this operation, each element of the matrix a is multiplied by matrix

            Breeze

        a :* b  
val d = a*b
println("Dot product a*b : n" + d)

The output of the preceding code is given as follows:

      Dot product a*b :
6 6
14 14
  • Elementwise comparison: In this operation, each element of a is compared with b. The code in Breeze is given as follows:
Breeze
        a :< b 

The output for the preceding code is give as follows:

      a :< b 
false false
false false
  • Inplace addition: This implies adding each element of a by 1.
Breeze
The following is the output of the preceding code:
      Inplace Addition : a :+= 1
2 3
4 5
value = a :+= 1
println("Inplace Addition : a :+= 1n" + e)
  • Element-wise sum: This is used to add all the elements of a matrix. The code in Breeze is given as follows:

            Breeze

        val sumA = sum(a) 
println("sum(a):n" + sumA)

The following is the output for the preceding code:

      sum(a):
14
  • Element-wise max: To find the maximum value out of all the elements in a matrix, we use
        a.max
Breeze
The code in Breeze can be written as follows:
        println("a.max:n" + a.max) 
  • Element-wise argmax: This is used to get the position of the element with the maximum value.

            Breeze

Code:
        println("argmax(a):n" + argmax(a)) 

The output of the preceding command is as follows:

      argmax(a):
(1,1)
  • Ceiling: This rounds off each element of the matrix to the next integer.
Breeze
Code:
        val g = DenseMatrix((1.1, 1.2), (3.9, 3.5)) 
println("g: n" + g)
val gCeil =ceil(g)
println("ceil(g)n " + gCeil)

The output of the preceding code is as follows:

      g: 
1.1 1.2
3.9 3.5

ceil(g)
2.0 2.0
4.0 4.0
  • Floor: Floor rounds off the value of each element to the nearest integer of a lower value.
Breeze
Code:
        val gFloor =floor(g) 
println("floor(g)n" + gFloor)

The output will be as follows:

      floor(g)
1.0 1.0
3.0 3.0
主站蜘蛛池模板: 棋牌| 阿勒泰市| 石嘴山市| 北碚区| 永新县| 阜新| 广东省| 工布江达县| 新营市| 通许县| 山阳县| 滨州市| 安国市| 普宁市| 宝清县| 六枝特区| 乌兰察布市| 象州县| 嘉义县| 乌拉特后旗| 惠东县| 边坝县| 金塔县| 西藏| 修水县| 介休市| 涞源县| 平度市| 荣昌县| 涟源市| 宜丰县| 同心县| 合水县| 滁州市| 南汇区| 安溪县| 遂川县| 景德镇市| 汶上县| 穆棱市| 永丰县|