红宝石 |矩阵元素()函数
element()是 Ruby 中的一个内置方法,它返回存在于第 i 行和第 j 列交叉处的元素。
Syntax: mat1.element(i, j)
Parameters: The function accepts two parameters i and j which signifies the row_number and column_number.
Return Value: It returns the element at mat[i][j].
示例 1 :
# Ruby program for element() method in Matrix
# Include matrix
require "matrix"
# Initialize a matrix
mat1 = Matrix[[1, 21], [31, 18]]
# prints the element at 1, 1
puts mat1.element(1, 1)
输出:
18
示例 2 :
# Ruby program for element() method in Matrix
# Include matrix
require "matrix"
# Initialize a matrix
mat1 = Matrix[[13, 1, 5], [12, 1, 5], [11, 2, 5]]
# prints the element at 0, 1
puts mat1.element(0, 1)
输出:
1