
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to Multiply to Matrix Using Multi-Dimensional Arrays
In this article, we will understand how to multiply to matrix using multi-dimensional arrays. The matrix has a row and column arrangement of its elements. A matrix with m rows and n columns can be called as m × n matrix.
Individual entries in the matrix are called element and can be represented by a[i][j] which suggests that the element a is present in the ith row and jth column.
Below is a demonstration of the same −
Suppose our input is −
First matrix: 2 3 4 5 2 3 4 6 9 Second matrix: 1 5 3 5 6 3 8 1 5
The desired output would be −
The product of two matrices is: 49 32 35 39 40 36 106 65 75
Algorithm
Step 1 - START Step 2 - Declare three integer matrices namely input_matrix_1, input_matrix_1 and resultant_matrix Step 3 - Define the values. Step 4 - Iterate over each element of the both the matrices using for-loop, multiply the element at [i][j] position of the first matrix with each element of the row of the second matrix and add the values, store the value at [i][j] position of the resultant matrix. Repeat this for each element of the first matrix. Step 5 - Display the result Step 5 - Stop
Example 1
Here, we bind all the operations together under the ‘main’ function.
public class MultiplyMatrices { public static void main(String[] args) { int matrix_size = 3; int[][] input_matrix_1 = { {2, 3, 4}, {5, 2, 3}, {4, 6, 9} }; System.out.println("The first matrix is defined as: "); for (int i = 0; i < matrix_size; i++) { for (int j = 0; j < matrix_size; j++) { System.out.print(input_matrix_1[i][j] + " "); } System.out.println(); } int[][] input_matrix_2 = { {1, 5, 3}, {5, 6, 3}, {8, 1, 5} }; System.out.println("The second matrix is defined as: "); for (int i = 0; i < matrix_size; i++) { for (int j = 0; j < matrix_size; j++) { System.out.print(input_matrix_2[i][j] + " "); } System.out.println(); } int[][] resultant_matrix = new int[matrix_size][matrix_size]; for(int i = 0; i < matrix_size; i++) { for (int j = 0; j < matrix_size; j++) { for (int k = 0; k < matrix_size; k++) { resultant_matrix[i][j] += input_matrix_1[i][k] * input_matrix_2[k][j]; } } } System.out.println("\n The product of two matrices is: "); for(int[] row : resultant_matrix) { for (int column : row) { System.out.print(column + " "); } System.out.println(); } } }
Output
The first matrix is defined as: 2 3 4 5 2 3 4 6 9 The second matrix is defined as: 1 5 3 5 6 3 8 1 5 The product of two matrices is: 49 32 35 39 40 36 106 65 75
Example 2
Here, we encapsulate the operations into functions exhibiting object-oriented programming.
public class MultiplyMatrices { static int matrix_size = 3; static void multiply(int input_matrix_1[][], int input_matrix_2[][]){ int[][] resultant_matrix = new int[matrix_size][matrix_size]; for(int i = 0; i < matrix_size; i++) { for (int j = 0; j < matrix_size; j++) { for (int k = 0; k < matrix_size; k++) { resultant_matrix[i][j] += input_matrix_1[i][k] * input_matrix_2[k][j]; } } } System.out.println("\n The product of two matrices is: "); for(int[] row : resultant_matrix) { for (int column : row) { System.out.print(column + " "); } System.out.println(); } } public static void main(String[] args) { int matrix_size = 3; int[][] input_matrix_1 = { {2, 3, 4}, {5, 2, 3}, {4, 6, 9} }; System.out.println("The first matrix is defined as: "); for (int i = 0; i < matrix_size; i++) { for (int j = 0; j < matrix_size; j++) { System.out.print(input_matrix_1[i][j] + " "); } System.out.println(); } int[][] input_matrix_2 = { {1, 5, 3}, {5, 6, 3}, {8, 1, 5} }; System.out.println("The second matrix is defined as: "); for (int i = 0; i < matrix_size; i++) { for (int j = 0; j < matrix_size; j++) { System.out.print(input_matrix_2[i][j] + " "); } System.out.println(); } multiply(input_matrix_1, input_matrix_2); } }
Output
The first matrix is defined as: 2 3 4 5 2 3 4 6 9 The second matrix is defined as: 1 5 3 5 6 3 8 1 5 The product of two matrices is: 49 32 35 39 40 36 106 65 75
- Related Articles
- Golang Program to Multiply to Matrix Using Multi-Dimensional Arrays
- Python Program to Multiply to Matrix Using Multi-dimensional Arrays
- C++ Program to Multiply Two Matrix Using Multi-dimensional Arrays
- Java Program to Add Two Matrix Using Multi-Dimensional Arrays
- Swift Program to Multiply two Matrices Using Multi-dimensional Arrays
- C++ Program to Add Two Matrix Using Multi-dimensional Arrays
- Swift Program to Subtract Two Matrix Using Multi-dimensional Arrays
- Golang Program to Add Two Matrix Using Multi-dimensional Arrays
- Swift Program to Add Two Matrix Using Multi-dimensional Arrays
- Java Program to convert array to String for one dimensional and multi-dimensional arrays
- Python Program to Add Two Matrix Using Multi-dimensional Array
- Dump Multi-Dimensional arrays in Java
- Does Java support multi-dimensional Arrays?
- How to Map multi-dimensional arrays to a single array in java?
- How to define multi-dimensional arrays in C#?

Advertisements