Java Tutorials for Freshers and Experience developers, Programming interview Questions, Data Structure and Algorithms interview Programs, Kotlin programs, String Programs, Java 8 Stream API, Spring Boot and Troubleshooting common issues.
Sunday, November 21, 2021
Java - How To Find Transpose Of A Matrix in Java in 4 ways?
Tuesday, October 27, 2020
Java Program to Find Transpose of a Matrix
1. Overview
In this article, you'll learn how to find the transpose of a given matrix using a simple for loop.
You can go thorough the previous articles on addition and multiplication of two matrices using arrays.
Transpose is nothing but a swapping the rows with columns and also order will be swapped. Finally, it produces the new matrix.
Matrix M : [A11, A12 A21, A22 A31, A32] Transpose of Matrix M: [ A11, A21, A31 A12, A22, A32]
Order of Transpose Matrix:
Matrix M order: 3 X 2
Transpose of Matrix M order: 2 X 3
Monday, October 26, 2020
Java Program to Multiply two Matrices by Passing Matrix to a Function
1. Overview
In this tutorial, you'll learn how to write the function for matrix multiplication. Let us pass the input and result matrices to this method and will run the core logic to multiply. Finally, the output is stored in the result array.
In the previous article, shown how to multiply two matrices without using function.
Example:
Matrix 1 order: 2 X 3
Matrix 2 order: 3 X 2
Result matrix order: 2 X 2
key note in the matrix multiplication is always matrix 1 columns size and matrix two row size must be equal. Otherwise matrix multiplication is not possible.
Sunday, October 25, 2020
Java Program to Multiply two Matrix Using Multi-dimensional Arrays
1. Overview
In this tutorial, you'll learn how to multiply two matrix in java using multi dimensional arrays.
To understand this example program, it is better to know the following concepts.
Arrays - How to Initialize Arrays
One condition must be satisfied for matrix multiplication as below.
First matrix order: R1 X C1
Second matrix order: R2 X C2
Always C1 and R2 must be same number that means number of columns in matrix 1 should be equal to the number of rows in the second matrix.
If this condition is not satisfied then matrix multiplication is not possible.
And also output matrix order will be R1 X C2.
Friday, October 23, 2020
Java Program to Add Two Matrix Using Multi-dimensional Arrays
1. Overview
In this tutorial, You'll learn how to add two matrix in java using arrays. You should know the basic mathematic problem solving which you learnt in the schooling level for Matrix Addition.
This can be implemented using two for loops easily in java programming.
In the previous article, we have shown how to multiply two matrices in java using threads.
2. Example Program To Add Two Matrices
Let us write a simple java program that takes two arrays as input and executes the core logic for addition. Finally, output array is printed onto the console.
Key note here is the order of two matrices should be same otherwise can not be computed the output.
package com.javaprogramto.programs.arrays.matrix; public class MatrixAddition { public static void main(String[] args) { // creating the first matric using arrays int[][] matrix1 = { { 1, 2, 3 }, { 4, 5, 6 } }; // creating the second matrix using two dimension array int[][] matrix2 = { { 1, 2, 3 }, { 4, 5, 6 } }; // output array for storing the addition result int[][] output = new int[matrix1.length][matrix2[1].length]; // matrix addition core logic for (int i = 0; i < matrix1.length; i++) { for (int j = 0; j < matrix2[1].length; j++) { output[i][j] = matrix1[i][j] + matrix2[i][j]; } } // printing the result for (int i = 0; i < output.length; i++) { for (int j = 0; j < output[1].length; j++) { System.out.print(output[i][j]+ " "); } System.out.println(); } } }
Output:
2 4 6 8 10 12
Here, First created two 2 dimensions arrays for storing the matrix1 and matrix2. Next, need to find the rows and columns using matrix1 and matrix 2.
We initialized the new array with the rows and columns size. This is used to store the result of addition.
Core logic is loop through the two matrices and add the values at the same index from both arrays.
At last, printed the output array using simple for loop.
3. Conclusion
In this article, you've seen how to find the addition of two matrices in java using arrays.
As usual, example shown is over GitHub.
Wednesday, January 22, 2020
Matrix Multiplication with Java Threads - Optimized Code (Parallel)
1. Introduction
In this tutorial, We will write the code to matrix multiplication in java using the normal approach and multiple threads in parallel. This question will be asked in many interview program questions to see whether can you improve the performance for large matrixes.
We'll implement the programs for both cases.
Basic Matrix Multiplication Ref
Matrix 1 order = m x n (m rows and n columns)
Matrix 2 order = n x p (n rows and p columns)
Result matrix order = m x p (m rows and p columns)
Here, we are assuming input is given accurately.