红宝石 |矩阵转置()函数
transpose()是 Ruby 中的一个内置方法,它返回矩阵的转置。
Syntax: mat1.transpose()
Parameters: The function needs the matrix to be transposed.
Return Value: It returns the transposed matrix.
示例 1 :
# Ruby program for transpose() method in Matrix
# Include matrix
require "matrix"
# Initialize a matrix
mat1 = Matrix[[3, 12], [2, 8]]
# Prints the transpose matrix
puts mat1.transpose()
输出:
Matrix[[3, 2], [12, 8]]
示例 2 :
# Ruby program for transpose() method in Matrix
# Include matrix
require "matrix"
# Initialize a matrix
mat1 = Matrix[[1, 0], [6, 1], [1, 2]]
# Prints the transpose matrix
puts mat1.transpose()
输出:
Matrix[[1, 6, 1], [0, 1, 2]]