Open In App

Ruby | Matrix scalar() function

Last Updated : 07 Jan, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
The scalar() is an inbuilt method in Ruby returns an N x N diagonal matrix where each diagonal element is value.
Syntax: mat1.scalar(N, value) Parameters: The function accepts twos mandatory parameters N and value where N is the size of the Identity matrix and value is the value to be assigned to the diagonals Return Value: It returns the diagonal matrix.
Example 1: Ruby
# Ruby program for scalar() method in Matrix
 
# Include matrix 
require "matrix"
 
# Initialize a matrix
# using scalar method 
mat1 = Matrix.scalar(2 ,6)
 
# Print the matrix
puts mat1
Output:
Matrix[[6, 0], [0, 6]]
Example 2: Ruby
# Ruby program for scalar() method in Matrix
 
# Include matrix 
require "matrix"
 
# Initialize a matrix
# using scalar method 
mat1 = Matrix.scalar(4 , 2)
 
# Print the matrix
puts mat1
Output:
Matrix[[2, 0, 0, 0], [0, 2, 0, 0], [0, 0, 2, 0], [0, 0, 0, 2]]

Next Article

Similar Reads