Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

matrix multiplication in java

    public class MatrixMultiplicationExample{  
    public static void main(String args[]){  
    //creating two matrices    
    int a[][]={{1,1,1},{2,2,2},{3,3,3}};    
    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();//new line    
    }    
    }}  
Comment

multiplication of two matrices java

// Java program to multiply two square
// matrices.
import java.io.*;
  
class GFG {
  
    static int N = 4;
  
    // This function multiplies mat1[][]
    // and mat2[][], and stores the result
    // in res[][]
    static void multiply(int mat1[][],
                         int mat2[][], int res[][])
    {
        int i, j, k;
        for (i = 0; i < N; i++) {
            for (j = 0; j < N; j++) {
                res[i][j] = 0;
                for (k = 0; k < N; k++)
                    res[i][j] += mat1[i][k]
                                 * mat2[k][j];
            }
        }
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int mat1[][] = { { 1, 1, 1, 1 },
                         { 2, 2, 2, 2 },
                         { 3, 3, 3, 3 },
                         { 4, 4, 4, 4 } };
  
        int mat2[][] = { { 1, 1, 1, 1 },
                         { 2, 2, 2, 2 },
                         { 3, 3, 3, 3 },
                         { 4, 4, 4, 4 } };
  
        // To store result
        int res[][] = new int[N][N];
        int i, j;
        multiply(mat1, mat2, res);
  
        System.out.println("Result matrix"
                           + " is ");
        for (i = 0; i < N; i++) {
            for (j = 0; j < N; j++)
                System.out.print(res[i][j]
                                 + " ");
            System.out.println();
        }
    }
}
  
// This code is contributed by anuj_67.
Comment

PREVIOUS NEXT
Code Example
Java :: how to add 2 numbers in java 
Java :: java split string by length 
Java :: base64 in java 
Java :: android play sound file from assets 
Java :: intent flag clear task 
Java :: java how to make a number 
Java :: add view to relativelayout programmatically 
Java :: java list distinct by attribute 
Java :: how to check if a string is numeric 
Java :: String remove duplicate in java 
Java :: return the maximum sum of two numbers whose digits add up to an equal sum 
Java :: junit vintage engine maven 
Java :: filtering out unique values from a list in java 
Java :: Index through 2d array 
Java :: check key is pressed java 
Java :: load file as string java 
Java :: next greater permutation leetcode 
Java :: date format in java 
Java :: get frequency of letters java 
Java :: java sleep 1 second 
Java :: declare array of chars java 
Java :: generate serial uuid with intelij 
Java :: gridlayout android studio 
Java :: array input java 
Java :: java booleans 
Java :: get string of radio button in android 
Java :: android studio download 
Java :: how to take max value from priority queue in java 
Java :: java long to hours minutes and seconds 
Java :: Program type already present: android.support.v4.app.INotificationSideChannel 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =