Open In App

Ruby | Matrix find_element() function

Improve
Improve
Like Article
Like
Save
Share
Report

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




# 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)


Output

1
0

Example 2:  

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)


Output

2
0

 


Last Updated : 14 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads