Ruby | Matrix find_element() function
The find_index is an inbuilt method in Ruby returns the index position of the given number. If the element is present more than once, the first occurrence is returned. If the element is not present, then nil is returned.
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.
Example 1:
Ruby
require "matrix"
mat1 = Matrix[[ 12 , 21 ], [ 31 , 12 ]]
puts mat1.find_index( 31 )
|
Output:
1
0
Example 2:
Ruby
require "matrix"
mat1 = Matrix[[ 6 , 7 ], [ 9 , 10 ], [ 12 , 4 ]]
puts mat1.find_index( 12 )
|
Output:
2
0
Last Updated :
14 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...