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

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
主站蜘蛛池模板: 怀化市| 隆昌县| 乌鲁木齐县| 江门市| 禹城市| 娱乐| 龙州县| 五台县| 安岳县| 怀柔区| 漠河县| 封开县| 大英县| 宁河县| 赣榆县| 郸城县| 定安县| 郸城县| 临邑县| 郸城县| 崇阳县| 武义县| 凌源市| 抚顺市| 崇文区| 兴仁县| 泸州市| 建始县| 平阳县| 达拉特旗| 海门市| 曲沃县| 石泉县| 宁德市| 彩票| 宾川县| 瑞安市| 康乐县| 惠安县| 贡觉县| 乡宁县|