coderheatsaxophone Posted May 30, 2022 Posted May 30, 2022 In one of my interview the interview asked me about Matrix Multiplication, wanted to understand how does this work? Any code example can anyone suggest me?
Sunila Jha Posted May 30, 2022 Posted May 30, 2022 (edited) You can use for-loop to implement Matrix Multiplication in Java import java.util.Scanner; public class MatrixMultiplicationExample{ public static void main(String args[]){ int row1, col1, row2, col2; Scanner s = new Scanner(System.in); System.out.print("Enter number of rows in first matrix:"); row1 = s.nextInt(); System.out.print("Enter number of columns in first matrix:"); col1 = s.nextInt(); System.out.print("Enter number of rows in second matrix:"); row2 = s.nextInt(); System.out.print("Enter number of columns in second matrix:"); col2 = s.nextInt(); if (col1 != row2) { System.out.println("Matrix multiplication is not possible"); } else { int a[][] = new int[row1][col1]; int b[][] = new int[row2][col2]; int c[][] = new int[row1][col2]; System.out.println("Enter values for matrix A : \n"); for (int i = 0; i < row1; i++) { for (int j = 0; j < col1; j++) a[i][j] = s.nextInt(); } System.out.println("Enter values for matrix B : \n"); for (int i = 0; i < row2; i++) { for (int j = 0; j < col2; j++) b[i][j] = s.nextInt(); } System.out.println("Matrix multiplication is : \n"); for(int i = 0; i < row1; i++) { for(int j = 0; j < col2; j++){ c[i][j]=0; for(int k = 0; k < col1; k++){ c[i][j] += a[i][k] * b[k][j]; } System.out.print(c[i][j] + " "); } System.out.println(); } } } } To know more about the Matrix Multiplication read more on commercial url removed by moderator. Edited July 7, 2022 by Phi for All No advertising, please.
sakshijn Posted July 6, 2022 Posted July 6, 2022 // *To perform matrix multiplication, the first matrix must have the same number of columns as the second matrix has rows. public class MatrixMultiplicationExample{ public static void main(String args[]){ //matrix 1 int a[][]={{1,1,1},{2,2,2},{3,3,3}}; //matrix 2 int b[][]={{1,1,1},{2,2,2},{3,3,3}}; //creating another matrix to store the multiplication of two matrices int c[][]=new int[3][3]; //3 rows and 3 columns //multiplying and printing multiplication of 2 matrices for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ c[i][j]=0; for(int k=0;k<3;k++) { c[i][j]+=a[i][k]*b[k][j]; }//end of k loop System.out.print(c[i][j]+" "); //printing matrix element }//end of j loop System.out.println(); } }} // Hope it will help you
gulshan212 Posted March 3, 2023 Posted March 3, 2023 Hello this is Gulshan Negi Well, matrix multiplication is a mathematical operation used to combine two matrices to produce a third matrix. It involves multiplying the elements of one matrix by the elements of another matrix and adding up the results. How matrix multiplication works: Suppose we have two matrices, A and B. Matrix A has dimensions m x n (m rows and n columns), while matrix B has dimensions n x p (n rows and p columns). To multiply these matrices, we need to make sure that the number of columns in matrix A is equal to the number of rows in matrix B. The resulting matrix, C, will have dimensions m x p (m rows and p columns). Each element of the resulting matrix is computed as the dot product of a row from matrix A and a column from matrix B. To find the element at the ith row and jth column of the resulting matrix, we would multiply the elements in the ith row of matrix A by the elements in the jth column of matrix B and add up the results. This can be represented mathematically as: C(i,j) = sum(A(i,k) * B(k,j)) for k=1 to n Codebase- def matrix_multiplication(A, B): m, n = A.shape n, p = B.shape C = np.zeros((m, p)) for i in range(m): for j in range(p): for k in range(n): C[i,j] += A[i,k] * B[k,j] return C where A and B are the input matrices, and np is the NumPy library used for matrix operations. Hope it will help you. Thanks
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now