Finding the entrywise product of two matrices.
Here’s a Java program that calculates the entrywise product of two matrices:
public class MatrixEntrywiseProduct { public static void main(String[] args) { int[][] matrix1 = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; int[][] matrix2 = { {9, 8, 7}, {6, 5, 4}, {3, 2, 1} }; int rows = matrix1.length; int columns = matrix1[0].length; int[][] result = new int[rows][columns]; // Calculate entrywise product for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { result[i][j] = matrix1[i][j] * matrix2[i][j]; } } // Print the result System.out.println("Entrywise Product:"); for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { System.out.print(result[i][j] + " "); } System.out.println(); } } }
In this example, we have two matrices `matrix1` and `matrix2`, both with dimensions 3×3. The program calculates the entrywise product of these two matrices and stores the result in the `result` matrix. Finally, it prints the resulting matrix.
You can modify the values of `matrix1` and `matrix2` to test the program with different matrices of the same dimensions.
No comments yet! You be the first to comment.