Java Plan To Multiply 2 Matrices - Matrix Multiplication Example

How to write a Java plan to multiply ii matrices inwards Java is a real skilful programming practise to acquire familiar amongst the two-dimensional array inwards Java. this instance teaches nearly how to multiply arrays, how to access elements from a multi-dimensional array, how to exceed them to a component etc. Since the matrix is a natural representation of multi-dimensional array inwards Java, they are oft used to illustrate existent give-and-take matrix exercises e.g. the calculating amount of ii matrices or calculating the departure of ii matrices etc. By the way, earlier writing the program, let's recap how to multiply ii matrices inwards mathematics first. If you lot remember, you lot tin exclusively multiply ii matrices if, together with exclusively if, the number of columns inwards the showtime matrix equals the number of rows inwards the minute matrix. That is known every bit matrix multiplication criterion.

If both matrices don't satisfy that measure together with then the production of ii matrices is undefined. The Product matrix's dimensions volition last equal to (rows of the showtime matrix) × (columns of the minute matrix ). For example, if nosotros multiply a 2×3 matrix amongst a 3×1 matrix, together with then the production matrix or number matrix volition last a 2×1 matrix i.e. ii rows together with 1 columns.

I used to recall this fox past times writing dimension of matrices side past times side to each other together with canceling their matching dimension e.g. if you lot write 2x3 together with 3x1, together with and then cancel three from each side you lot volition acquire a matrix of dimension 2x1, which is basically the dimension of production matrix.



How to multiply ii matrices inwards Java

Here is a graphical representation of matrix multiplication algorithm:

 How to write a Java plan to multiply ii matrices inwards Java is a real skilful programming e Java Program to Multiply Two Matrices - Matrix Multiplication Example

You tin run across that both matrices met the status for multiplication i.e. columns of the showtime matrix are equal to rows of the minute matrix. Then nosotros multiply the showtime row of the showtime matrix to the showtime column of the minute matrix together with this gives us the showtime chemical constituent of the showtime column of number matrix. Similarly, when you lot multiply the minute row of the showtime matrix to the showtime column of the minute matrix you lot acquire the minute chemical constituent of the showtime column inwards the number matrix.


How to multiply ii matrices inwards Java- Program
Here is our consummate Java plan to perform matrix multiplication. In this program, nosotros showtime inquire the user to acquire inwards ii matrices. Since you cannot bring array from the ascendence business inwards Java (see here), nosotros inquire the user to showtime acquire inwards the number of rows together with columns of the matrix together with and then inquire him to populate the matrix.


Once nosotros receive got both the matrices gear upwards nosotros showtime cheque whether they met the status of matrix multiplication or non i.e. number of columns of the showtime matrix matches to the rows of the minute matrix. As I said, nosotros receive got used a two-dimensional array to stand upwards for a matrix inwards Java.

import java.util.Scanner;  /** * Java plan to calculate production of Two matrices inwards Java. In social club to * multiply ii matrices, column of showtime matrix must last equal to rows of the minute * matrix. * * @author Javin Paul */ public class MatrixMultiplication{      public static void main(String args[]) {          Scanner cmd = new Scanner(System.in);         System.out.println("Enter the number of rows together with columns of                               showtime matrix");          int rowsOfFirstMatrix = cmd.nextInt();         int columnsOfFirstMatrix = cmd.nextInt();         int[][] aMatrix = new int[rowsOfFirstMatrix][columnsOfFirstMatrix];          System.out.println("Enter the elements of showtime matrix");         for (int i = 0; i < rowsOfFirstMatrix; i++) {             for (int j = 0; j < columnsOfFirstMatrix; j++) {                 aMatrix[i][j] = cmd.nextInt();             }         }          System.out.println("Enter the number of rows together with columns of the                                 minute matrix");         int rowsOfSecondMatrix = cmd.nextInt();         int columnsOfSecondMatrix = cmd.nextInt();          // security internet - cheque social club or each matrix, whether eligible for         // multiplication or not         while (columnsOfFirstMatrix != rowsOfSecondMatrix) {             System.out.printf("Matrices amongst entered orders can't last                          multiplied amongst each other, "                    + "columnsOfFirstMatrix [%d] != rowsOfSecondMatrix [%d] %n",                     columnsOfFirstMatrix, rowsOfSecondMatrix);             System.out.println("Enter the number of rows together with columns of                                     minute matrix");             rowsOfSecondMatrix = cmd.nextInt();             columnsOfSecondMatrix = cmd.nextInt();         }          int[][] bMatrix = new int[rowsOfSecondMatrix][columnsOfSecondMatrix];         System.out.println("Enter numbers of minute matrix");         for (int i = 0; i < rowsOfSecondMatrix; i++) {             for (int j = 0; j < columnsOfSecondMatrix; j++) {                 bMatrix[i][j] = cmd.nextInt();             }         }          // calculating production of ii matrices inwards Java         int[][] production = product(aMatrix, bMatrix);         System.out.println("Product of entered matrices:-");          for (int i = 0; i < rowsOfFirstMatrix; i++) {             for (int j = 0; j < columnsOfSecondMatrix; j++) {                 System.out.printf("%d ", product[i][j]);             }             System.out.printf("%n");         }         cmd.close();     }      /**      * Method to calculate multiplication or production of ii matrices.      *      * @param matrix1      * @param matrix2      * @return production of ii matrix      */     public static int[][] product(int[][] matrix1, int[][] matrix2) {         int columnsOfFirstMatrix = matrix1[0].length;         int rowsOfSecondMatrix = matrix2.length;          if (columnsOfFirstMatrix != rowsOfSecondMatrix) {             throw new IllegalArgumentException(String.format("Can't multiply                       matrices, columns of showtime matrix"                     + " %d is non equal to rows of minute matrix %d",                        columnsOfFirstMatrix, rowsOfSecondMatrix));         }          int rowsOfFirstMatrix = matrix1.length;         int columnsofSecondMatrix = matrix2[0].length;         int[][] production = new int[rowsOfFirstMatrix][columnsofSecondMatrix];          for (int i = 0; i < rowsOfFirstMatrix; i++) {             for (int j = 0; j < columnsofSecondMatrix; j++) {                  int amount = 0;                 for (int k = 0; k < rowsOfSecondMatrix; k++) {                     amount = amount + matrix1[i][k] * matrix2[k][j];                 }                  product[i][j] = sum;             }         }          return product;     }  } 


together with hither is the output of this plan when you lot volition run this on your favorite Java IDE e.g. Eclipse or IntelliJIDEA or simply from the ascendence prompt:

Output: Enter the number of rows and columns of the first matrix 2 3 Enter the elements of the first matrix 1 2 3 4 5 6 Enter the number of rows and columns of the second matrix 2 4 Matrices amongst entered orders can't last multiplied amongst each other,  columnsOfFirstMatrix [3] != rowsOfSecondMatrix [2] Enter the number of rows and columns of the second matrix 3 2 Enter numbers of the second matrix 7 8 9 10 11 12 The production of entered matrices:- 58 64 139 154

You tin run across that our showtime instance was non perfect, the matrices nosotros entered cannot last multiplied amongst each other because columns of the showtime matrix are non equal to rows of the minute matrix.

That's all nearly how to create matrix multiplication inwards Java. This is a skilful programming practise to larn together with empathize how to purpose two-dimensional arrays inwards Java, which is i of the cardinal information structure, peculiarly for game evolution field. If you lot are simply starting amongst programming together with non familiar amongst cardinal programming concepts together with then I likewise propose you lot read Head First Java, which teaches you lot basics of programming inwards Java language.



Other Java Programming exercises for beginners
  • How to transpose Matrix inwards Java? (program)
  • How to count vowels together with consonants inwards given String inwards Java? (solution)
  • How to contrary an array inwards house inwards Java? (solution)
  • How to contrary a String inwards house inwards Java? (solution)
  • How to cheque if ii rectangles intersect amongst each other inwards Java? (solution)
  • How to implement Linear Search inwards Java? (solution)
  • How to impress Fibonacci serial inwards Java (solution)
  • How to cheque if given String is palindrome or non inwards Java? (solution)
  • How to remove duplicate characters from String inwards Java? (solution)
  • How to cheque if a twelvemonth is a boundary twelvemonth inwards Java? (solution)
  • How to contrary words inwards a given String inwards Java? (solution)
  • How to cheque if given number is prime number inwards Java (solution)
  • How to calculate Area of Triangle inwards Java? (program)
  • How to cheque if ii given Strings are Anagram inwards Java? (solution)
  • How to calculate the foursquare origin of a given number inwards Java? (solution)
  • How to calculate the amount of all elements of an array inwards Java? (program)
  • How to implement binary search using recursion inwards Java? (solution)
  • How to uncovering if given Integer is Palindrome inwards Java? (solution)
  • How to uncovering all permutations of a given String inwards Java? (solution)
  • How to cheque if a String contains duplicate characters inwards Java? (solution)
  • How to remove duplicate elements from the array inwards Java? (solution)
  • How to uncovering the highest occurring give-and-take from a given file in Java? (solution)
  • How to calculate the average of all numbers of an array inwards Java? (program)


Further Learning
The Coding Interview Bootcamp: Algorithms + Data Structures
Data Structures together with Algorithms: Deep Dive Using Java
Algorithms together with Data Structures - Part 1 together with 2


Sumber https://javarevisited.blogspot.com/

0 Response to "Java Plan To Multiply 2 Matrices - Matrix Multiplication Example"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel