红宝石 |矩阵+方法
+是 Ruby 中的一个内置方法,它返回一个矩阵,其中添加了两个矩阵 mat1 和 mat2。
Syntax: mat1 + mat2
Parameters: The function need two matrix mat1 and mat2 which are to be added.
Return Value: It returns the resultant matrix after addition.
示例 1 :
# Ruby program for + method in Matrix
# Include matrix
require "matrix"
# Initialize a matrix
mat1 = Matrix[[1, 2, 6], [3, 4, 8], [12, 1, 3]]
mat2 = Matrix[[1, 2, 6], [3, 4, 8], [12, 1, 3]]
# Prints the value of mat1+mat2
puts mat1 + mat2
输出:
Matrix[[2, 4, 12], [6, 8, 16], [24, 2, 6]]
示例 2 :
# Ruby program for + method in Matrix
# Include matrix
require "matrix"
# Initialize a matrix
mat1 = Matrix[[1, 21, 6], [31,124, 18]]
mat2 = Matrix[[1, 12, 16], [31, 43, 28]]
# Prints the value of mat1+mat2
puts mat1 + mat2
输出:
Matrix[[2, 33, 22], [62, 167, 46]]