红宝石 |矩阵 to_a()函数
to_a()是 Ruby 中的一个内置方法,它返回一个数组,其中包含矩阵的所有元素。
Syntax: mat1.to_a()
Parameters: The function needs the matrix which is to be converted to an array.
Return Value: It returns an array.
示例 1 :
# Ruby program for to_a() method in Matrix
# Include matrix
require "matrix"
# Initialize a matrix
mat1 = Matrix[[3, 12], [2, 8]]
# Prints the to_a matrix
puts mat1.to_a()
输出:
3
12
2
8
示例 2 :
# Ruby program for to_a() method in Matrix
# Include matrix
require "matrix"
# Initialize a matrix
mat1 = Matrix[[1, 0], [6, 1], [1, 2]]
# converted to array
arr = mat1.to_a()
# Prints the array
puts arr
输出:
1
0
6
1
1
2