红宝石 |矩阵 hadamard_product()函数
hadamard_product?()是 Ruby 中的一个内置方法,返回两个矩阵的 hadamard_product。它采用两个相同维度的矩阵并生成另一个与操作数相同维度的矩阵,其中每个元素 i, j 是原始两个矩阵的元素 i, j 的乘积。
Syntax: mat1.hadamard_product(mat2)
Parameters: The function needs two matrix whose hadamard_product is to be done.
Return Value: It returns the hadamard_product of two matrices.
示例 1 :
# Ruby program for hadamard_product() method in Matrix
# Include matrix
require "matrix"
# Initialize a matrix
mat1 = Matrix[[1, 21], [31, 18]]
mat2 = Matrix[[1, 3], [3, 4]]
# prints hadamard_product
puts mat1.hadamard_product(mat2)
输出:
Matrix[[1, 63], [93, 72]]
示例 2 :
# Ruby program for hadamard_product() method in Matrix
# Include matrix
require "matrix"
# Initialize a matrix
mat1 = Matrix[[1, 31], [13, 28], [3, 4]]
mat2 = Matrix[[32, 3], [32, 74], [43, 3]]
# prints hadamard_product
puts mat1.hadamard_product(mat2)
输出:
Matrix[[32, 93], [416, 2072], [129, 12]]