Open In App

Ruby | Matrix vstack() function

Last Updated : 07 Jan, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The vstack() is an inbuilt method in Ruby returns a new matrix resulting by stacking vertically the receiver with the given matrices.
It requires a matrix which is stacked upon vertically.

Syntax: mat1.vstack(mat2)

Parameters: The function needs a matrix which is to be stacked vertically.

Return Value: It returns resultant matrix after stacking is done.

Example 1:




# Ruby program for vstack() 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.vstack(mat2)


Output:

Matrix[[1, 21], [31, 18], [4, 6], [3, 9]]

Example 2:




# Ruby program for vstack() method in Matrix
   
# Include matrix 
require "matrix"
   
# Initialize a matrix 
mat1 =  Matrix[[3, 5, 9], [10, 19, 123]]
mat2 =  Matrix[[12, 12, 13], [19, 18, 89]]
   
# prints the resultant matrix 
puts  mat1.vstack(mat2)


Output:

Matrix[[3, 5, 9], [10, 19, 123], [12, 12, 13], [19, 18, 89]]

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads