红宝石 |矩阵 hstack()函数
hstack()是 Ruby 中的一个内置方法,通过将接收器与给定矩阵水平堆叠来返回一个新矩阵。
它需要一个水平堆叠的矩阵。
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.
示例 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)
输出:
Matrix[[1, 21, 4, 6], [31, 18, 3, 9]]
示例 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)
输出:
Matrix[[3, 5, 9, 12, 12], [10, 19, 123, 19, 18]]