红宝石 |矩阵 I()函数
I()是 Ruby 中的一个内置方法,它返回一个 NXN 大小的单位矩阵。
Syntax: mat1.I(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 I() method in Matrix
# Include matrix
require "matrix"
# Initialize a matrix
# using I method
mat1 = Matrix.I(2)
# Print the matrix
puts mat1
输出:
Matrix[[1, 0], [0, 1]]
示例 2 :
# Ruby program for I() method in Matrix
# Include matrix
require "matrix"
# Initialize a matrix
# using I method
mat1 = Matrix.I(3)
# Print the matrix
puts mat1
输出:
Matrix[[1, 0, 0], [0, 1, 0], [0, 0, 1]]