Ruby 矩阵 transpose()函数
transpose() 是Ruby中一个内置的方法,用于返回矩阵的转置。
语法 :mat1.transpose()
参数 :该函数需要被转置的矩阵。
返回值 :它返回转置后的矩阵。
例子 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]]