Open In App

Ruby | Matrix identity() function

Last Updated : 12 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report
The identity() is an inbuilt method in Ruby returns an Identity matrix of N X N size.
Syntax: mat1.identity(N) Parameters: The function accepts a mandatory parameter N which is the size of the Identity matrix. Return Value: It returns the Identity matrix.
Example 1: Ruby
# Ruby program for identity() method in Matrix

# Include matrix 
require "matrix"

# Initialize a matrix
# using identity method 
mat1 = Matrix.identity(2)

# Print the matrix
puts mat1
Output:
Matrix[[1, 0], [0, 1]]
Example 2: Ruby
# Ruby program for identity() method in Matrix

# Include matrix 
require "matrix"

# Initialize a matrix
# using identity method 
mat1 = Matrix.identity(3)

# Print the matrix
puts mat1
Output:
Matrix[[1, 0, 0], [0, 1, 0], [0, 0, 1]]

Similar Reads