Finding the matrix cosine.
Here’s an example of a Java problem that demonstrates how to find the cosine of a matrix:
import java.util.Arrays; public class MatrixCosine { public static void main(String[] args) { double[][] matrix = { {1.0, 2.0, 3.0}, {4.0, 5.0, 6.0}, {7.0, 8.0, 9.0} }; double[][] cosineMatrix = findMatrixCosine(matrix); System.out.println("Original Matrix:"); printMatrix(matrix); System.out.println("Cosine Matrix:"); printMatrix(cosineMatrix); } public static double[][] findMatrixCosine(double[][] matrix) { int rows = matrix.length; int cols = matrix[0].length; double[][] cosineMatrix = new double[rows][cols]; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { cosineMatrix[i][j] = Math.cos(matrix[i][j]); } } return cosineMatrix; } public static void printMatrix(double[][] matrix) { for (double[] row : matrix) { System.out.println(Arrays.toString(row)); } System.out.println(); } }
In this example, we have a 2D array `matrix` representing a matrix of values. The `findMatrixCosine` method calculates the cosine of each element in the matrix and stores the results in another matrix called `cosineMatrix`. The `printMatrix` method is used to display the original matrix and the cosine matrix.
When you run the `main` method, it will output the original matrix and the corresponding cosine matrix. You can modify the values in the `matrix` array to test the program with different input matrices.
No comments yet! You be the first to comment.