红宝石 |矩阵 find_element()函数
find_index是 Ruby 中的一个内置方法,它返回给定数字的索引位置。如果元素多次出现,则返回第一次出现。如果元素不存在,则返回 nil。
Syntax: mat1.find_index(element)
Parameters: The function needs an element which is to be searched in matrix mat1.
Return Value: It returns the row number and column number where the element is.
示例 1 :
Ruby
# Ruby program for find_index method in Matrix
# Include matrix
require "matrix"
# Initializes the matrix
mat1 = Matrix[[12, 21], [31, 12]]
# Prints the index of 31
puts mat1.find_index(31)
Ruby
# Ruby program for find_index method in Matrix
# Include matrix
require "matrix"
# Initializes the matrix
mat1 = Matrix[[6, 7], [9, 10], [12, 4]]
# Prints the index of 12
puts mat1.find_index(12)
输出:
1
0
示例 2 :
红宝石
# Ruby program for find_index method in Matrix
# Include matrix
require "matrix"
# Initializes the matrix
mat1 = Matrix[[6, 7], [9, 10], [12, 4]]
# Prints the index of 12
puts mat1.find_index(12)
输出:
2
0