Ruby 矩阵 – 方法
在Ruby中,” – “是一个内置的方法,用于返回两个矩阵mat1和mat2的相减结果的矩阵。
语法 :mat1 – mat2
参数 :该函数需要两个要被减去的矩阵mat1和mat2。
返回值 :它返回减法后的结果矩阵。
例子 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[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
例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[[0, 9, -10], [0, 81, -10]]