Finding the matrix entry wise product using element-wise multiplication
Here’s a Java program that finds the entrywise product of two matrices using element-wise multiplication:
public class MatrixEntrywiseProduct { public static int[][] entrywiseProduct(int[][] matrix1, int[][] matrix2) { int rows = matrix1.length; int columns = matrix1[0].length; int[][] result = new int[rows][columns]; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { result[i][j] = matrix1[i][j] * matrix2[i][j]; } } return result; } public static void main(String[] args) { int[][] matrix1 = { {1, 2, 3}, {4, 5, 6} }; int[][] matrix2 = { {2, 3, 4}, {5, 6, 7} }; int[][] productMatrix = entrywiseProduct(matrix1, matrix2); System.out.println("Matrix Entrywise Product:"); for (int i = 0; i < productMatrix.length; i++) { for (int j = 0; j < productMatrix[0].length; j++) { System.out.print(productMatrix[i][j] + " "); } System.out.println(); } } }
In this program, the `entrywiseProduct` method takes two matrices as input and returns a new matrix that contains the entrywise product of the input matrices. The `main` method demonstrates how to use the `entrywiseProduct` method by providing two example matrices, `matrix1` and `matrix2`. The resulting product matrix is then printed to the console.
Note that the matrices used in the example are 2×3 matrices, but you can modify the code to work with matrices of different dimensions by adjusting the values in the `matrix1` and `matrix2` arrays.
No comments yet! You be the first to comment.