Showing posts with label Matrix. Show all posts
Showing posts with label Matrix. Show all posts

Determine if a given Matrix is a Sparse Matrix in Java



Note: In numerical analysis, a sparse matrix is amatrix in which most of the elements are zero. By contrast, if most of the elements are nonzero, then the matrix is considered dense. The fraction of zero elements over the total number of elements in a matrix is called the sparsity (density).

public class SparceMatrix {

    public static void main(String args[]) {
        int a[][] = new int[3][3];
        int count=0;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter element of Matrix ");
        for (int i = 0; i <= 2; i++) 
        {
            for (int j = 0; j <= 2; j++) 
            {
                a[i][j] = sc.nextInt();
            }
        }
        System.out.println("Value of Matrix is ");
        for (int i = 0; i <= 2; i++)
        {
            for (int j = 0; j <= 2; j++) 
            {
                System.out.print(a[i][j] + " ");
            }
            System.out.println();
        }

        for (int i = 0; i <= 2; i++) 

        {
            for (int j = 0; j <= 2; j++) 
            {
                if(a[i][j]==0)
                {
                count++;
                }
            }
        }

        if (count>((3*3)/2)) 

        {
            System.out.println("Given Matrix is Sparce Matrix");
        } else 
        {
            System.out.println("Given Matrix is not Sparce Matrix");
        }
    }
}


Output: 


(First Run..)


Enter element of Matrix 

1
0
2
0
0
3
4
0
0

Value of Matrix is 

1 0 2 
0 0 3 
4 0 0 

Given Matrix is Sparce Matrix .


(Second Run)


Enter element of Matrix 

1
2
0
0
5
5
0
4
4

Value of Matrix is 

1 2 0 
0 5 5 
0 4 4 

Given Matrix is not Sparce Matrix 



BUILD SUCCESSFUL (total time: 10 seconds)




Find the Frequency of Odd & Even Numbers in the given Matrix in Java



Note: 1- An odd number is an integer which is not a multiple of two. If it is divided by two the result is a fraction.
2- The even numbers are all numbers that are multiples of 2.


public class CountEvenOdd {
    public static void main(String args[]) {
        int a[][] = new int[3][3];
        int noOfOdd=0;
        int noOfEven=0;


        Scanner sc = new Scanner(System.in);


        System.out.println("Enter the element of Matrix ");

        for (int i = 0; i <= 2; i++) 
        {

            for (int j = 0; j <= 2; j++) 

            {
                a[i][j] = sc.nextInt();

            }

        }
        System.out.println("Value of  First Matrix is ");
        for (int i = 0; i <= 2; i++)
        {

            for (int j = 0; j <= 2; j++) 

            {
                System.out.print(a[i][j] + " ");

            }

            System.out.println();
        }
        
        for (int i = 0; i <= 2; i++)
        {

            for (int j = 0; j <= 2; j++) 

            {
                if(a[i][j]%2==0)
                {
                noOfEven++;
                }
                else
                {
                noOfOdd++;
                }

            }

            
        }
        
        System.out.println("Tota even number "+noOfEven);
        System.out.println("Tota odd number "+noOfOdd);
    }

    

}

Output:

Enter the element of Matrix 
1
2
3
4
5
6
7
8
9

Value of  First Matrix is 
1 2 3 
4 5 6 
7 8 9 

Tota even number 4
Tota odd number 5


BUILD SUCCESSFUL (total time: 7 seconds)



Check if a given Matrix is an Identity (Unit) Matrix or not in Java



Note: Identity matrix is a square matrix with 1′s along the diagonal from upper left to lower right and 0′s in all other positions. If it satisfies the structure as explained before then the matrix is called as identity matrix.

public class IdentityMatrix {

    public static void main(String args[]) {

        int a[][] = new int[3][3];
        int flag = 1;

        Scanner sc = new Scanner(System.in);


        System.out.println("Enter elements of Matrix ");

        for (int i = 0; i <= 2; i++) 
        {

            for (int j = 0; j <= 2; j++) 

            {
                a[i][j] = sc.nextInt();

            }

        }
        System.out.println("Value of  First Matrix is ");
        for (int i = 0; i <= 2; i++)
        {

            for (int j = 0; j <= 2; j++) 

            {
                System.out.print(a[i][j] + " ");

            }

            System.out.println();
        }

        for (int i = 0; i <= 2; i++) 

        {

            for (int j = 0; j <= 2; j++) 

            {
                if (i == j) 
                {
                    if (a[i][j] != 1) 
                    {
                        flag = 0;
                        break;
                    }
                } else 
                {
                    if (a[i][j] != 0) 
                    {
                        flag = 0;
                        break;
                    }
                }

            }


        }


        if (flag == 1) 

        {
            System.out.println("It is identity matrix \n");
        } else 
        {
            System.out.println("It is not a identity matrix \n");
        }
    }

}



Output:

(run-1)

Enter First Matrix 
1
0
0
0
1
0
0
0
1
Value of  First Matrix is 
1 0 0 
0 1 0 
0 0 1 
It is identity matrix 

BUILD SUCCESSFUL (total time: 8 seconds)

Output:

(run-2)


Enter First Matrix 
1
0
1
0
1
1
0
0
0
Value of  First Matrix is 
1 0 1 
0 1 1 
0 0 0 
It is not a identity matrix 

BUILD SUCCESSFUL (total time: 10 seconds)




Calculate the Sum of the Elements of each Row & Column of given Matrix in Java.



public class SumOfEachRowAndColumn {

    public static void main(String args[]) 

    {
        int a[][] = new int[3][3];
        int sumOfRow = 0;
        int sumOfColumn = 0;
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter element of Matrix ");

        for (int i = 0; i <= 2; i++) 
        {

            for (int j = 0; j <= 2; j++)

            {
                a[i][j] = sc.nextInt();

            }

        }
        System.out.println("Value of  Matrix is ");
        for (int i = 0; i <= 2; i++) 
        {

            for (int j = 0; j <= 2; j++)

            {
                System.out.print(a[i][j] + " ");

            }

            System.out.println();
        }
        
        //Clculating the sum of rows

        for (int i = 0; i <= 2; i++)

        {

            for (int j = 0; j <= 2; j++)

            {
                sumOfRow = sumOfRow + a[i][j];

            }

            System.out.println("Sum of " + (i + 1) + " row is " + sumOfRow);
            sumOfRow = 0;
        }

        //Clculating the sum of column


        for (int i = 0; i <= 2; i++)

        {

            for (int j = 0; j <= 2; j++)

            {
                sumOfColumn = sumOfColumn + a[j][i];

            }

            System.out.println("Sum of " + (i + 1) + " column is " +             sumOfColumn);
            sumOfColumn = 0;
        }

    }


}



Output:

Enter element of Matrix 
1
2
3
4
5
6
1
2
3

Value of  Matrix is 
1 2 3 
4 5 6 
1 2 3 

Sum of 1 row is 6
Sum of 2 row is 15
Sum of 3 row is 6

Sum of 1 column is 6
Sum of 2 column is 9
Sum of 3 column is 12

BUILD SUCCESSFUL (total time: 7 seconds)




Program for Matrix multiplication in Java.



Note:  Step 1: Make sure that the the number of columns in the 1st one equals the number of rows in the 2nd one. (The pre-requisite to be able to multiply) 

Step 2: Multiply the elements of each row of the first matrix by the elements of each column in the second matrix. 

Step 3: Add the products.


public class MatrixMultiplication {

    public static void main(String args[]) {
        int a[][] = new int[3][3];
        int b[][] = new int[3][3];
        int mul[][] = new int[3][3];
        int sum = 0;
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter element of First Matrix ");
        for (int i = 0; i <= 2; i++) {

            for (int j = 0; j <= 2; j++) {
                a[i][j] = sc.nextInt();

            }
        }
        System.out.println("Value of  Matrix is ");
        for (int i = 0; i <= 2; i++) {

            for (int j = 0; j <= 2; j++) {
                System.out.print(a[i][j] + " ");

            }
            System.out.println();
        }

        System.out.println("Enter element of Second Matrix ");
        for (int i = 0; i <= 2; i++) {

            for (int j = 0; j <= 2; j++) {
                b[i][j] = sc.nextInt();

            }
        }
        System.out.println("Value of Second Matrix is ");
        for (int i = 0; i <= 2; i++) {

            for (int j = 0; j <= 2; j++) {
                System.out.print(b[i][j] + " ");

            }
            System.out.println();
        }

        //Logic for matrix multiplication

        for (int k = 0; k < 3; k++) 
        {
            for (int i = 0; i < 3; i++) 
            {
                for (int j = 0; j < 3; j++) 
                {
                    sum=sum+(a[k][j]*b[j][i]);
                }
                mul[k][i]=sum;
                sum=0;
            }
        }
        
        System.out.println("Multiplication of both Matrix is ");
        for (int i = 0; i <= 2; i++) {

            for (int j = 0; j <= 2; j++) {
                System.out.print(mul[i][j] + " ");

            }
            System.out.println();
        }

    }

}

Output:

Enter element of First Matrix 
1
2
3
4
5
6
7
8
9

Value of  Matrix is 
1 2 3 
4 5 6 
7 8 9 

Enter element of Second Matrix 
1
2
3
4
5
6
7
8
9

Value of Second Matrix is 
1 2 3 
4 5 6 
7 8 9 

Multiplication of both Matrix is 
30 36 42 
66 81 96 
102 126 150 







Find the Transpose of given Matrix in Java.



Note: The transpose of a matrix is a new matrix whose rows are the columns of the original. (This makes the columns of the new matrix the rows of the original). 

public class TransposeOfMatrix {
    public static void main(String args[]) {
        int a[][] = new int[3][3];
        int transpose[][] = new int[3][3];
        Scanner sc=new Scanner(System.in);

        System.out.println("Enter Matrix ");
        for (int i = 0; i <= 2; i++) 
        {
            
            for (int j = 0; j <= 2; j++) 
            {
                a[i][j]=sc.nextInt();

            }
        }
        System.out.println("Value of  First Matrix is ");
        for (int i = 0; i <= 2; i++) 
        {
            
            for (int j = 0; j <= 2; j++) 
            {
                System.out.print(a[i][j]+" ");

            }
            System.out.println();
        }
        for (int i = 0; i <= 2; i++) 
        {
            
            for (int j = 0; j <= 2; j++) 
            {
                transpose[i][j]=a[j][i];

            }
        }
        
        System.out.println("Transpose of given Matrix is ");
        for (int i = 0; i <= 2; i++) 
        {
            
            for (int j = 0; j <= 2; j++) 
            {
                System.out.print(transpose[i][j]+" ");

            }
            System.out.println();
        }
        
    }
    
}


Output:

Enter Matrix 
1
2
3
1
2
3
1
2
3

Value of  First Matrix is 
1 2 3 
1 2 3 
1 2 3 

Transpose of given Matrix is 
1 1 1 
2 2 2 
3 3 3 




Addition , Substraction and Trace of two Matrix in Java.



Note: In linear algebra, the trace of an n-by-n square matrix A is defined to be the sum of the elements on the main diagonal (the diagonal from the upper left to the lower right) of A,


public class MatrixAddAndSub {
    public static void main(String args[]) {
        int a[][] = new int[3][3];
        int b[][] = new int[3][3];
        int add[][] = new int[3][3];
        int sub[][] = new int[3][3];
        int trace=0;
        Scanner sc=new Scanner(System.in);

        System.out.println("Enter First Matrix ");
        for (int i = 0; i <= 2; i++)
        {
           
            for (int j = 0; j <= 2; j++)
            {
                a[i][j]=sc.nextInt();

            }
        }
        System.out.println("Value of  First Matrix is ");
        for (int i = 0; i <= 2; i++)
        {
           
            for (int j = 0; j <= 2; j++)
            {
                System.out.print(a[i][j]+" ");

            }
            System.out.println();
        }
       
        System.out.println("Enter Second Matrix ");
        for (int i = 0; i <= 2; i++)
        {
           
            for (int j = 0; j <= 2; j++)
            {
                b[i][j]=sc.nextInt();

            }
        }
        System.out.println("Value of Second Matrix is ");
        for (int i = 0; i <= 2; i++)
        {
           
            for (int j = 0; j <= 2; j++)
            {
                System.out.print(b[i][j]+" ");

            }
            System.out.println();
        }
       
        for (int i = 0; i <= 2; i++)
        {
           
            for (int j = 0; j <= 2; j++)
            {
                add[i][j]=a[i][j]+b[i][j];

            }
         
        }
       
        System.out.println("Addition of Matrix is ");
        for (int i = 0; i <= 2; i++)
        {
           
            for (int j = 0; j <= 2; j++)
            {
                System.out.print(add[i][j]+" ");

            }
            System.out.println();
        }
       
        for (int i = 0; i <= 2; i++)
        {
           
            for (int j = 0; j <= 2; j++)
            {
                sub[i][j]=a[i][j]-b[i][j];

            }
         
        }
       
        System.out.println("Substraction of Matrix is ");
        for (int i = 0; i <= 2; i++)
        {
           
            for (int j = 0; j <= 2; j++)
            {
                System.out.print(sub[i][j]+" ");

            }
            System.out.println();
        }
       
        // Tracing Logic
       
        for (int i = 0; i <= 2; i++)
        {
           
            for (int j = 0; j <= 2; j++)
            {
                if(i==j)
                {
                trace=trace+a[i][j];
                }

            }
         
        }
        System.out.println("Trace of Matrix A is  "+trace);
       
       
    }

}


Output:

Enter First Matrix 
1
2
3
1
2
3
1
2
3
Value of  First Matrix is 
1 2 3 
1 2 3 
1 2 3 
Enter Second Matrix 
1
2
3
1
2
3
1
2
3
Value of Second Matrix is 
1 2 3 
1 2 3 
1 2 3 
Addition of Matrix is 
2 4 6 
2 4 6 
2 4 6 
Substraction of Matrix is 
0 0 0 
0 0 0 
0 0 0 

Trace of Matrix A is  6