In the previous article, we have discussed Java Program to Find Unique Elements in a Matrix
In this article we are going to see how we can check if a matrix is a markov matrix or not in JAVA language.
Java Program to Check Whether the Matrix is a Markov Matrix or Not
A 3*3 Matrix is having 3 rows and 3 columns where this 3*3 represents the dimension of the matrix. Means there are 3*3 i.e. total 9 elements in a 3*3 Matrix.
Let’s understand it in more simpler way.
| A00 A01 A02 |
Matrix A = | A10 A11 A12 |
| A20 A21 A22 | 3*3
Matrix Arepresents a 3*3 matrix.- ‘
A‘ represents the matrix element - ‘
Aij‘ represents the matrix element at it’s matrix position/index. - ‘
i‘ represents the row index - ‘
j‘ represents the column index - Means
A00=Aijwherei=0andj=0,A01=aijwherei=0andj=1and like this. - Here we have started
rowvalue from 0 andcolumnvalue from 0.
Let’s see different ways to check Markov Matrix.
Markov matrix is a matrix whose all rows add upto 1.
Example: 1 0 0 0 1 0 0 0 0
Method-1: Java Program to Check Markov Matrix By Static Initialization of Array Elements
Approach:
- Initialize and declare one array of size 3×3 with elements.
- Add each row elements and check if it is equal to 1.
- If all the rows sum equals to 1, then it is a Markov matrix.
Program:
public class matrix
{
public static void main(String args[])
{
// Initializing the 3X3 matrix i.e. 2D array
double arr[][] = {{0.5,0,0.5},{0.5,0.5,0},{1,0,0}};
int row, col;
System.out.print("The matrix elements are : ");
printMatrix(arr);
if(checkMarkov(arr))
System.out.println("\nIt is a Markov matrix");
else
System.out.println("\nIt is not a Markov matrix");
}
// Method to print the matrix
static void printMatrix(double arr[][])
{
int row, col;
// Loop to print the elements
for(row=0;row<3;row++)
{
// Used for formatting
System.out.print("\n");
for(col=0;col<3;col++)
{
System.out.print(arr[row][col]+" ");
}
}
System.out.print("\n");
}
// Checks whether the matrix is a Markov matrix or not
static boolean checkMarkov(double arr[][])
{
int row, col;
double sum;
for(row =0;row<3;row++)
{
sum =0;
for(col=0;col<3;col++)
sum+=arr[row][col];
if(sum!=1)
return false;
}
return true;
}
}
Output: The matrix elements are : 0.5 0.0 0.5 0.5 0.5 0.0 1.0 0.0 0.0 It is a Markov matrix
Method-2: Java Program to Check Markov Matrix By Dynamic Initialization of Array Elements
Approach:
- Declare one array of size 3×3.
- Use two for loops to input array elements.
- Add each row elements and check if it is equal to 1.
- If all the rows sum equals to 1, then it is a Markov matrix.
Program:
import java.util.Scanner;
public class matrix{
public static void main(String args[])
{
//Scanner class to take input
Scanner scan = new Scanner(System.in);
// Initializing the 3X3 matrix i.e. 2D array
int arr[][] = new int[3][3];
int row, col;
// Taking matrix input
System.out.println("\nEnter matrix elements : ");
for(row=0;row<3;row++)
for(col=0;col<3;col++)
arr[row][col] = scan.nextInt();
System.out.print("The matrix elements are:");
printMatrix(arr);
if(checkMarkov(arr))
System.out.println("\nIt is a Markov matrix");
else
System.out.println("\nIt is not a Markov matrix");
}
// Method to print the matrix
static void printMatrix(int arr[][])
{
int row, col;
// Loop to print the elements
for(row=0;row<3;row++)
{
// Used for formatting
System.out.print("\n");
for(col=0;col<3;col++)
{
System.out.print(arr[row][col]+" ");
}
}
System.out.print("\n");
}
// Checks whether the matrix is a Markov matrix or not
static boolean checkMarkov(int arr[][])
{
int row, col;
double sum;
for(row =0;row<3;row++)
{
sum =0;
for(col=0;col<3;col++)
sum+=arr[row][col];
if(sum!=1)
return false;
}
return true;
}
}
Output: Enter matrix elements : 1 0 0 0 0 1 0 1 0 The matrix elements are: 1 0 0 0 0 1 0 1 0 It is a Markov matrix
Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.
Related Java Programs: