Java Plan To Multiply 2 Matrices - Matrix Multiplication Example

Java Plan To Multiply 2 Matrices - Matrix Multiplication Example - Hallo sahabat BEST LEARNING JAVA, Pada Artikel yang anda baca kali ini dengan judul Java Plan To Multiply 2 Matrices - Matrix Multiplication Example, kami telah mempersiapkan artikel ini dengan baik untuk anda baca dan ambil informasi didalamnya. mudah-mudahan isi postingan Artikel Coding Interview Question, Artikel Coding problems, Artikel core java, Artikel programming, yang kami tulis ini dapat anda pahami. baiklah, selamat membaca.

Judul : Java Plan To Multiply 2 Matrices - Matrix Multiplication Example
link : Java Plan To Multiply 2 Matrices - Matrix Multiplication Example

Baca juga


Java Plan To Multiply 2 Matrices - Matrix Multiplication Example

How to write a Java programme to multiply 2 matrices inward Java is a real skillful programming practice to acquire familiar amongst the two-dimensional array inward Java. this instance teaches well-nigh how to multiply arrays, how to access elements from a multi-dimensional array, how to top them to a component division etc. Since the matrix is a natural representation of multi-dimensional array inward Java, they are oft used to illustrate existent give-and-take matrix exercises e.g. the calculating total of 2 matrices or calculating the departure of 2 matrices etc. By the way, earlier writing the program, let's recap how to multiply 2 matrices inward mathematics first. If y'all remember, y'all tin exclusively multiply 2 matrices if, in addition to exclusively if, the publish of columns inward the outset matrix equals the publish of rows inward the minute matrix. That is known every bit matrix multiplication criterion.

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

I used to holler back this describe a fast 1 on yesteryear writing dimension of matrices following to each other in addition to canceling their matching dimension e.g. if y'all write 2x3 in addition to 3x1, in addition to and then cancel three from each side y'all volition acquire a matrix of dimension 2x1, which is basically the dimension of production matrix.



How to multiply 2 matrices inward Java

Here is a graphical representation of matrix multiplication algorithm:

 How to write a Java programme to multiply 2 matrices inward Java is a real skillful programming e Java Program to Multiply Two Matrices - Matrix Multiplication Example

You tin come across that both matrices met the status for multiplication i.e. columns of the outset matrix are equal to rows of the minute matrix. Then nosotros multiply the outset row of the outset matrix to the outset column of the minute matrix in addition to this gives us the outset chemical ingredient of the outset column of consequence matrix. Similarly, when y'all multiply the minute row of the outset matrix to the outset column of the minute matrix y'all acquire the minute chemical ingredient of the outset column inward the consequence matrix.


How to multiply 2 matrices inward Java- Program
Here is our consummate Java programme to perform matrix multiplication. In this program, nosotros outset enquire the user to come inward 2 matrices. Since you cannot convey array from the ascendancy line of piece of job inward Java (see here), nosotros enquire the user to outset come inward the publish of rows in addition to columns of the matrix in addition to and then enquire him to populate the matrix.


Once nosotros conduct keep both the matrices laid upward nosotros outset cheque whether they met the status of matrix multiplication or non i.e. publish of columns of the outset matrix matches to the rows of the minute matrix. As I said, nosotros conduct keep used a two-dimensional array to stand upward for a matrix inward Java.

import java.util.Scanner;  /** * Java programme to calculate production of Two matrices inward Java. In guild to * multiply 2 matrices, column of outset matrix must locomote 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 publish of rows in addition to columns of                               outset matrix");          int rowsOfFirstMatrix = cmd.nextInt();         int columnsOfFirstMatrix = cmd.nextInt();         int[][] aMatrix = new int[rowsOfFirstMatrix][columnsOfFirstMatrix];          System.out.println("Enter the elements of outset 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 publish of rows in addition to columns of the                                 minute matrix");         int rowsOfSecondMatrix = cmd.nextInt();         int columnsOfSecondMatrix = cmd.nextInt();          // security cyberspace - cheque guild or each matrix, whether eligible for         // multiplication or not         while (columnsOfFirstMatrix != rowsOfSecondMatrix) {             System.out.printf("Matrices amongst entered orders can't locomote                          multiplied amongst each other, "                    + "columnsOfFirstMatrix [%d] != rowsOfSecondMatrix [%d] %n",                     columnsOfFirstMatrix, rowsOfSecondMatrix);             System.out.println("Enter the publish of rows in addition to 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 2 matrices inward 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 2 matrices.      *      * @param matrix1      * @param matrix2      * @return production of 2 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 outset 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 total = 0;                 for (int k = 0; k < rowsOfSecondMatrix; k++) {                     total = total + matrix1[i][k] * matrix2[k][j];                 }                  product[i][j] = sum;             }         }          return product;     }  } 


in addition to hither is the output of this programme when y'all volition run this on your favorite Java IDE e.g. Eclipse or IntelliJIDEA or simply from the ascendancy prompt:

Output: Enter the publish 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 publish of rows and columns of the second matrix 2 4 Matrices amongst entered orders can't locomote multiplied amongst each other,  columnsOfFirstMatrix [3] != rowsOfSecondMatrix [2] Enter the publish 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 come across that our outset instance was non perfect, the matrices nosotros entered cannot locomote multiplied amongst each other because columns of the outset matrix are non equal to rows of the minute matrix.

That's all well-nigh how to produce matrix multiplication inward Java. This is a skillful programming practice to larn in addition to sympathise how to role two-dimensional arrays inward Java, which is 1 of the cardinal information structure, peculiarly for game evolution field. If y'all are simply starting amongst programming in addition to non familiar amongst cardinal programming concepts in addition to then I likewise advise y'all read Head First Java, which teaches y'all basics of programming inward Java language.



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


Further Learning
The Coding Interview Bootcamp: Algorithms + Data Structures
Data Structures in addition to Algorithms: Deep Dive Using Java
Algorithms in addition to Data Structures - Part 1 in addition to 2



Demikianlah Artikel Java Plan To Multiply 2 Matrices - Matrix Multiplication Example

Sekianlah artikel Java Plan To Multiply 2 Matrices - Matrix Multiplication Example kali ini, mudah-mudahan bisa memberi manfaat untuk anda semua. baiklah, sampai jumpa di postingan artikel lainnya.

Anda sekarang membaca artikel Java Plan To Multiply 2 Matrices - Matrix Multiplication Example dengan alamat link https://bestlearningjava.blogspot.com/2019/04/java-plan-to-multiply-2-matrices-matrix.html

Belum ada Komentar untuk "Java Plan To Multiply 2 Matrices - Matrix Multiplication Example"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel