Java Plan To Multiply 2 Matrices - Matrix Multiplication Example
How to write a Java plan to multiply 2 matrices inwards Java is a really practiced programming exercise to acquire familiar alongside the two-dimensional array inwards Java. this instance teaches nigh how to multiply arrays, how to access elements from a multi-dimensional array, how to transcend them to a business office etc. Since the matrix is a natural representation of multi-dimensional array inwards Java, they are ofttimes used to illustrate existent give-and-take matrix exercises e.g. the calculating amount 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 inwards mathematics first. If yous remember, yous tin flame exclusively multiply 2 matrices if, too exclusively if, the lay out of columns inwards the starting fourth dimension matrix equals the lay out of rows inwards the minute matrix. That is known every bit matrix multiplication criterion.
If both matrices don't satisfy that measure too hence the production of 2 matrices is undefined. The Product matrix's dimensions volition last equal to (rows of the starting fourth dimension matrix) × (columns of the minute matrix ). For example, if nosotros multiply a 2×3 matrix alongside a 3×1 matrix, too hence the production matrix or trial matrix volition last a 2×1 matrix i.e. 2 rows too 1 columns.
I used to retrieve this fob past times writing dimension of matrices side past times side to each other too canceling their matching dimension e.g. if yous write 2x3 too 3x1, too and hence cancel three from each side yous volition acquire a matrix of dimension 2x1, which is basically the dimension of production matrix.
You tin flame meet that both matrices met the status for multiplication i.e. columns of the starting fourth dimension matrix are equal to rows of the minute matrix. Then nosotros multiply the starting fourth dimension row of the starting fourth dimension matrix to the starting fourth dimension column of the minute matrix too this gives us the starting fourth dimension chemical component of the starting fourth dimension column of trial matrix. Similarly, when yous multiply the minute row of the starting fourth dimension matrix to the starting fourth dimension column of the minute matrix yous acquire the minute chemical component of the starting fourth dimension column inwards the trial matrix.
How to multiply 2 matrices inwards Java- Program
Here is our consummate Java plan to perform matrix multiplication. In this program, nosotros starting fourth dimension enquire the user to acquire inwards 2 matrices. Since you cannot convey array from the dominance trouble inwards Java (see here), nosotros enquire the user to starting fourth dimension acquire inwards the lay out of rows too columns of the matrix too and hence enquire him to populate the matrix.
Once nosotros receive got both the matrices railroad train nosotros starting fourth dimension banking concern check whether they met the status of matrix multiplication or non i.e. lay out of columns of the starting fourth dimension matrix matches to the rows of the minute matrix. As I said, nosotros receive got used a two-dimensional array to stand upward for a matrix inwards Java.
too hither is the output of this plan when yous volition run this on your favorite Java IDE e.g. Eclipse or IntelliJIDEA or merely from the dominance prompt:
You tin flame meet that our starting fourth dimension instance was non perfect, the matrices nosotros entered cannot last multiplied alongside each other because columns of the starting fourth dimension matrix are non equal to rows of the minute matrix.
That's all nigh how to create matrix multiplication inwards Java. This is a practiced programming exercise to larn too sympathise how to piece of occupation two-dimensional arrays inwards Java, which is 1 of the cardinal information structure, specially for game evolution field. If yous are merely starting alongside programming too non familiar alongside cardinal programming concepts too hence I likewise advise yous read Head First Java, which teaches yous basics of programming inwards Java language.
Other Java Programming exercises for beginners
Further Learning
The Coding Interview Bootcamp: Algorithms + Data Structures
Data Structures too Algorithms: Deep Dive Using Java
Algorithms too Data Structures - Part 1 too 2
If both matrices don't satisfy that measure too hence the production of 2 matrices is undefined. The Product matrix's dimensions volition last equal to (rows of the starting fourth dimension matrix) × (columns of the minute matrix ). For example, if nosotros multiply a 2×3 matrix alongside a 3×1 matrix, too hence the production matrix or trial matrix volition last a 2×1 matrix i.e. 2 rows too 1 columns.
I used to retrieve this fob past times writing dimension of matrices side past times side to each other too canceling their matching dimension e.g. if yous write 2x3 too 3x1, too and hence cancel three from each side yous volition acquire a matrix of dimension 2x1, which is basically the dimension of production matrix.
How to multiply 2 matrices inwards Java
Here is a graphical representation of matrix multiplication algorithm:You tin flame meet that both matrices met the status for multiplication i.e. columns of the starting fourth dimension matrix are equal to rows of the minute matrix. Then nosotros multiply the starting fourth dimension row of the starting fourth dimension matrix to the starting fourth dimension column of the minute matrix too this gives us the starting fourth dimension chemical component of the starting fourth dimension column of trial matrix. Similarly, when yous multiply the minute row of the starting fourth dimension matrix to the starting fourth dimension column of the minute matrix yous acquire the minute chemical component of the starting fourth dimension column inwards the trial matrix.
How to multiply 2 matrices inwards Java- Program
Here is our consummate Java plan to perform matrix multiplication. In this program, nosotros starting fourth dimension enquire the user to acquire inwards 2 matrices. Since you cannot convey array from the dominance trouble inwards Java (see here), nosotros enquire the user to starting fourth dimension acquire inwards the lay out of rows too columns of the matrix too and hence enquire him to populate the matrix.
Once nosotros receive got both the matrices railroad train nosotros starting fourth dimension banking concern check whether they met the status of matrix multiplication or non i.e. lay out of columns of the starting fourth dimension matrix matches to the rows of the minute matrix. As I said, nosotros receive got used a two-dimensional array to stand upward for a matrix inwards Java.
import java.util.Scanner; /** * Java plan to calculate production of Two matrices inwards Java. In social club to * multiply 2 matrices, column of starting fourth dimension 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 lay out of rows too columns of starting fourth dimension matrix"); int rowsOfFirstMatrix = cmd.nextInt(); int columnsOfFirstMatrix = cmd.nextInt(); int[][] aMatrix = new int[rowsOfFirstMatrix][columnsOfFirstMatrix]; System.out.println("Enter the elements of starting fourth dimension 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 lay out of rows too columns of the minute matrix"); int rowsOfSecondMatrix = cmd.nextInt(); int columnsOfSecondMatrix = cmd.nextInt(); // security cyberspace - banking concern check social club or each matrix, whether eligible for // multiplication or not while (columnsOfFirstMatrix != rowsOfSecondMatrix) { System.out.printf("Matrices alongside entered orders can't last multiplied alongside each other, " + "columnsOfFirstMatrix [%d] != rowsOfSecondMatrix [%d] %n", columnsOfFirstMatrix, rowsOfSecondMatrix); System.out.println("Enter the lay out of rows too 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 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 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 starting fourth dimension 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; } }
too hither is the output of this plan when yous volition run this on your favorite Java IDE e.g. Eclipse or IntelliJIDEA or merely from the dominance prompt:
Output: Enter the lay out 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 lay out of rows and columns of the second matrix 2 4 Matrices alongside entered orders can't last multiplied alongside each other, columnsOfFirstMatrix [3] != rowsOfSecondMatrix [2] Enter the lay out 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 flame meet that our starting fourth dimension instance was non perfect, the matrices nosotros entered cannot last multiplied alongside each other because columns of the starting fourth dimension matrix are non equal to rows of the minute matrix.
That's all nigh how to create matrix multiplication inwards Java. This is a practiced programming exercise to larn too sympathise how to piece of occupation two-dimensional arrays inwards Java, which is 1 of the cardinal information structure, specially for game evolution field. If yous are merely starting alongside programming too non familiar alongside cardinal programming concepts too hence I likewise advise yous read Head First Java, which teaches yous basics of programming inwards Java language.
Other Java Programming exercises for beginners
- How to transpose Matrix inwards Java? (program)
- How to count vowels too 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 banking concern check if 2 rectangles intersect alongside each other inwards Java? (solution)
- How to implement Linear Search inwards Java? (solution)
- How to impress Fibonacci serial inwards Java (solution)
- How to banking concern check if given String is palindrome or non inwards Java? (solution)
- How to remove duplicate characters from String inwards Java? (solution)
- How to banking concern check if a twelvemonth is a trammel twelvemonth inwards Java? (solution)
- How to contrary words inwards a given String inwards Java? (solution)
- How to banking concern check if given lay out is prime number inwards Java (solution)
- How to calculate Area of Triangle inwards Java? (program)
- How to banking concern check if 2 given Strings are Anagram inwards Java? (solution)
- How to calculate the foursquare origin of a given lay out 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 banking concern check 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 too Algorithms: Deep Dive Using Java
Algorithms too Data Structures - Part 1 too 2


Komentar
Posting Komentar