Ruby | Matrix hadamard_product() function

The hadamard_product?() is an inbuilt method in Ruby returns the hadamard_product of two matrices. It takes two matrices of the same dimensions and produces another matrix of the same dimension as the operands where each element i, j is the product of elements i, j of the original two matrices.

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.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output:

Matrix[[1, 63], [93, 72]]

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output:

Matrix[[32, 93], [416, 2072], [129, 12]]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.