Jump to content

Featured Replies

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 by Phi for All
No advertising, please.

  • 1 month later...
// *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

 

Please sign in to comment

You will be able to leave a comment after signing in

Sign In Now

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.