Open In App

Ruby | Matrix hstack() function

Improve
Improve
Like Article
Like
Save
Share
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:




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




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

Last Updated : 07 Jan, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads