红宝石 |矩阵零?()函数
zero?()是 Ruby 中的内置方法,返回一个布尔值。如果它是零矩阵,则返回 true,否则返回 false。零矩阵意味着矩阵的所有元素都为0。
Syntax: mat1.zero?()
Parameters: The function needs the matrix to be checked for zero matrix.
Return Value: It returns true if it is a zero matrix, else it returns false.
示例 1 :
# Ruby program for zero?() method in Matrix
# Include matrix
require "matrix"
# Initialize a matrix
mat1 = Matrix[[1, 21], [31, 18]]
# Prints if zero matrix or not
puts mat1.zero?()
输出:
false
示例 2 :
# Ruby program for zero?() method in Matrix
# Include matrix
require "matrix"
# Initialize a matrix
mat1 = Matrix[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
# Prints if zero or not
puts mat1.zero?()
输出:
true