Open In App

Ruby | Matrix hstack() function

Last Updated : 07 Jan, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
The hstack() is an inbuilt method in Ruby returns a new matrix resulting by stacking horizontally the receiver with the given matrices. It requires a matrix which is stacked upon horizontally.
Syntax: mat1.hstack(mat2) Parameters: The function needs a matrix which is to be stacked horizontally. Return Value: It returns resultant matrix after stacking is done.
Example 1: CPP
#Ruby program for hstack() method in Matrix

#Include matrix
require "matrix"

#Initialize a matrix
    mat1
    = Matrix[[ 1, 21 ], [ 31, 18 ]] mat2 = Matrix[[ 4, 6 ], [ 3, 9 ]]

#prints the resultant matrix
                                           puts mat1.hstack(mat2)
Output:
Matrix[[1, 21, 4, 6], [31, 18, 3, 9]]
Example 2: CPP
#Ruby program for hstack() method in Matrix

#Include matrix
require "matrix"

#Initialize a matrix
    mat1
    = Matrix[[ 3, 5, 9 ], [ 10, 19, 123 ]] mat2 = Matrix[[ 12, 12 ], [ 19, 18 ]]

#prints the resultant matrix
                                                  puts mat1.hstack(mat2)
Output:
Matrix[[3, 5, 9, 12, 12], [10, 19, 123, 19, 18]]

Next Article

Similar Reads