红宝石 |矩阵单元()函数
unit()是 Ruby 中的一个内置方法,它返回一个 NXN 大小的单位矩阵。
Syntax: mat1.unit(N)
Parameters: The function accepts a mandatory parameter N which is the size of the Identity matrix.
Return Value: It returns the Identity matrix.
示例 1 :
# Ruby program for unit() method in Matrix
# Include matrix
require "matrix"
# Initialize a matrix
# using unit method
mat1 = Matrix.unit(2)
# Print the matrix
puts mat1
输出:
Matrix[[1, 0], [0, 1]]
示例 2 :
# Ruby program for unit() method in Matrix
# Include matrix
require "matrix"
# Initialize a matrix
# using unit method
mat1 = Matrix.unit(3)
# Print the matrix
puts mat1
输出:
Matrix[[1, 0, 0], [0, 1, 0], [0, 0, 1]]