红宝石 |矩阵[]方法
[]()是 Ruby 中的内置方法,返回第 i 行第 j 列的元素。
Syntax: mat1[i, j]
Parameters: The function needs the row number i and column number j whose position element is to be returned.
Return Value: It returns the element at the position mat[i][j].
示例 1 :
# Ruby program for [] method in Matrix
# Include matrix
require "matrix"
# Initialize a matrix
mat1 = Matrix[[1, 21], [31, 18]]
# Print the element at 1,1
puts mat1[1, 1]
输出:
18
示例 2 :
# Ruby program for [] method in Matrix
# Include matrix
require "matrix"
# Initialize a matrix
mat1 = Matrix[[89 , 12, 12], [8, 5, 23], [1, 2, 9]]
# Print the element at 2,3
puts mat1[2, 2]
输出:
9