Ruby | Matrix [] method

The []() is an inbuilt method in Ruby returns the element at the i-th row and the j-th column.

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].

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

18

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

9
My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.