Ruby | Matrix [] method Last Updated : 12 Jul, 2025 Comments Improve Suggest changes Like Article Like Report 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: Ruby # 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] Output: 18 Example 2: Ruby # 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] Output: 9 Comment More infoAdvertise with us Next Article Ruby | Matrix ** method G gopaldave Follow Improve Article Tags : Ruby Ruby-Methods Ruby Matrix-class Similar Reads Ruby | Matrix ** method The ** is an inbuilt method in Ruby returns the matrix after the matrix is multiplied with self N times. It returns the matrix exponentiation value. Syntax: Matrix_name ** (number) Parameters: The function takes a mandatory parameter number which signifies the number of times it will be multiplied w 1 min read Ruby | Matrix - method The - is an inbuilt method in Ruby returns a matrix which has the subtraction of two matrix mat1 and mat2. Syntax: mat1 - mat2 Parameters: The function need two matrix mat1 and mat2 which are to be subtracted. Return Value: It returns the resultant matrix after subtraction. Example 1: Ruby # Ruby pr 1 min read Ruby | Matrix + method The + is an inbuilt method in Ruby returns a matrix which has the addition of two matrix mat1 and mat2. Syntax: mat1 + mat2 Parameters: The function need two matrix mat1 and mat2 which are to be added. Return Value: It returns the resultant matrix after addition. Example 1: Ruby # Ruby program for + 1 min read Ruby | Matrix == method The == is an inbuilt method in Ruby returns a boolean value. It returns true if both the matrix are equal or else it returns false.. Syntax: mat1 == mat2 Parameters: The function need two matrix mat1 and mat2 which are to be compared. Return Value: It returns true if both the matrix are equal or els 1 min read Ruby | Matrix division / method The / is an inbuilt method in Ruby returns a matrix which has the division of two matrix mat1 and mat2. Here, division means multiplication with the inverse. Syntax: mat1 / mat2 Parameters: The function need two matrix mat1 and mat2 which are to be divided. Return Value: It returns the resultant ma 1 min read Ruby | Matrix rect() function The rect() is an inbuilt method in Ruby returns two matrix. The first matrix returned has all the real values of the matrix, and the second matrix has all the imaginary values in the matrix. Syntax: mat1.rect() Parameters: The function needs the matrix whose real and imaginary values are to be retur 1 min read Like