Lập trình Java - Cộng, Trừ, Nhân, Chuyển vị Ma trận

Cộng, Trừ, Nhân, Chuyển vị ma trận

Trong bài này chúng ta viết chương trình Cộng, Trừ, Nhân, Chia 2 ma trận

1. Cộng 2 ma trận

Xem lưu đồ sau cộng 2 ma trận sau:

Mã nguồn cộng 2 ma trận:

 int[][] resultMatix = new int[rows][columns];
           for (int i = 0; i < rows; i++) {
                  for (int j = 0; j < columns; j++) {
                        resultMatix[i][j] = matrix1[i][j] + matrix2[i][j];
                  }
           }

Toàn bộ mã nguồn cộng 2 ma trận


import java.util.Scanner;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author ADMIN
 */
public class MatrixAddition {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner scanner = new Scanner(System.in);
       System.out.println("Enter number of rows in matrix : "); //rows and columns in matrix1 and matrix2 must be same for addition.
       int rows = scanner.nextInt();
       System.out.println("Enter number of columns in matrix : ");
       int columns = scanner.nextInt();
       int[][] matrix1 = new int[rows][columns];
       int[][] matrix2 = new int[rows][columns];

       System.out.println("Enter the elements in first matrix :");
       for (int i = 0; i < rows; i++) {
              for (int j = 0; j < columns; j++) {
                    matrix1[i][j] = scanner.nextInt();
              }
       }
       System.out.println("Enter the elements in second matrix :");
       for (int i = 0; i < rows; i++) {
              for (int j = 0; j < columns; j++) {
                    matrix2[i][j] = scanner.nextInt();
              }
       }

       //addition of matrices.
       int[][] resultMatix = new int[rows][columns];
       for (int i = 0; i < rows; i++) {
              for (int j = 0; j < columns; j++) {
                    resultMatix[i][j] = matrix1[i][j] + matrix2[i][j];
              }
       }

       System.out.println("\nFirst matrix is : ");
       for (int i = 0; i < rows; i++) {
              for (int j = 0; j < columns; j++) {
                    System.out.print(matrix1[i][j] + " ");
              }
              System.out.println();
       }
 
           System.out.println("\nSecond matrix is : ");
           for (int i = 0; i < rows; i++) {
                  for (int j = 0; j < columns; j++) {
                        System.out.print(matrix2[i][j] + " ");
                  }
                  System.out.println();
           }
 
           System.out.println("\nThe sum of the two matrices is : ");
           for (int i = 0; i < rows; i++) {
                  for (int j = 0; j < columns; j++) {
                        System.out.print(resultMatix[i][j] + " ");
                  }
                  System.out.println();
           }
    }
    
}

Kết quả khi chạy chương trình:

Enter number of rows in matrix : 
3
Enter number of columns in matrix : 
3
Enter the elements in first matrix :
1
2
3
4
5
6
7
8
9
Enter the elements in second matrix :
3
3
2
4
5
6
7
8
9

First matrix is : 
1 2 3 
4 5 6 
7 8 9 

Second matrix is : 
3 3 2 
4 5 6 
7 8 9 

The sum of the two matrices is : 
4 5 5 
8 10 12 
14 16 18 

2. Trừ 2 ma trận

Xem lưu đồ sau trừ 2 ma trận sau:

Mã nguồn trừ 2 ma trận:

int[][] resultMatix = new int[rows][columns];
           for (int i = 0; i < rows; i++) {
                  for (int j = 0; j < columns; j++) {
                        resultMatix[i][j] = matrix1[i][j] - matrix2[i][j];
                  }
           }

Toàn bộ mã nguồn chương trình trừ 2 ma trận:


import java.util.Scanner;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author ADMIN
 */
public class MatrixSubtraction {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
       Scanner scanner = new Scanner(System.in);
       System.out.println("Enter number of rows in matrix : "); //rows and columns in matrix1 and matrix2 must be same for subtraction.
       int rows = scanner.nextInt();
       System.out.println("Enter number of columns in matrix : ");
       int columns = scanner.nextInt();
       int[][] matrix1 = new int[rows][columns];
       int[][] matrix2 = new int[rows][columns];

       System.out.println("Enter the elements in first matrix :");
       for (int i = 0; i < rows; i++) {
              for (int j = 0; j < columns; j++) {
                    matrix1[i][j] = scanner.nextInt();
              }
       }
       System.out.println("Enter the elements in second matrix :");
       for (int i = 0; i < rows; i++) {
              for (int j = 0; j < columns; j++) {
                    matrix2[i][j] = scanner.nextInt();
              }
       }

       //Subtraction of matrices.
       int[][] resultMatix = new int[rows][columns];
       for (int i = 0; i < rows; i++) {
              for (int j = 0; j < columns; j++) {
                    resultMatix[i][j] = matrix1[i][j] - matrix2[i][j];
              }
       }

       System.out.println("\nFirst matrix is : ");
       for (int i = 0; i < rows; i++) {
              for (int j = 0; j < columns; j++) {
                    System.out.print(matrix1[i][j] + " ");
              }
              System.out.println();
       }

       System.out.println("\nSecond matrix is : ");
       for (int i = 0; i < rows; i++) {
              for (int j = 0; j < columns; j++) {
                    System.out.print(matrix2[i][j] + " ");
              }
              System.out.println();
       }

       System.out.println("\nThe subtraction of the two matrices is : ");
       for (int i = 0; i < rows; i++) {
              for (int j = 0; j < columns; j++) {
                    System.out.print(resultMatix[i][j] + " ");
              }
              System.out.println();
           }
    }
    
}

Kết quả khi chạy chương trình:

Enter number of rows in matrix : 
3
Enter number of columns in matrix : 
3
Enter the elements in first matrix :
1
2
3
4
5
6
7
8
9
Enter the elements in second matrix :
9
8
7
6
5
4
3
2
1

First matrix is : 
1 2 3 
4 5 6 
7 8 9 

Second matrix is : 
9 8 7 
6 5 4 
3 2 1 

The subtraction of the two matrices is : 
-8 -6 -4 
-2 0 2 
4 6 8 

3. Nhân 2 ma trận

Xem lưu đồ sau nhân 2 ma trận sau:

Mã nguồn nhân 2 ma trận:

int[][] productMatrix  = new int[rowsMatrix1][columnsMatrix2];
      for (int i = 0; i < rowsMatrix1; i++) {
        for (int j = 0; j < columnsMatrix2; j++) {
        for (int k = 0; k < columnsMatrix1RowsMatrix2; k++) { //columns in matrix1= rows in matrix2
            productMatrix[i][j]= productMatrix[i][j] + matrix1[i][k] *matrix2[k][j];
          }
           }
   }

Toàn bộ mã nguồn nhân 2 ma trận:


import java.util.Scanner;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author ADMIN
 */
public class MatrixMultiplication {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("Enter number of rows in first matrix : ");
        int rowsMatrix1 = scanner.nextInt();
        System.out.println("Enter number of columns in first matrix / rows in matrix2: ");
        int columnsMatrix1RowsMatrix2 = scanner.nextInt();         //variable name used for understanding convenience, because columns in matrix1 = rows in matrix2
        System.out.println("Enter number of columns in second matrix : ");
        int columnsMatrix2 = scanner.nextInt();
        
        int[][] matrix1 = new int[rowsMatrix1][columnsMatrix1RowsMatrix2];
        int[][] matrix2 = new int[columnsMatrix1RowsMatrix2][columnsMatrix2];
        System.out.println("Enter the first matrix in elements :");
        for (int i = 0; i < matrix1.length; i++) {
            for (int j = 0; j < matrix1[0].length; j++) {
                matrix1[i][j] = scanner.nextInt();
            }
        }
        
        System.out.println("Enter the second matrix elements:");
        for (int i = 0; i < matrix2.length; i++) {
            for (int j = 0; j < matrix2[0].length; j++) {
                matrix2[i][j] = scanner.nextInt();
            }
        }
        
        
        //Multiply matrices
        int[][] productMatrix  = new int[rowsMatrix1][columnsMatrix2];
        for (int i = 0; i < rowsMatrix1; i++) {
            for (int j = 0; j < columnsMatrix2; j++) {
                for (int k = 0; k < columnsMatrix1RowsMatrix2; k++) { //columns in matrix1= rows in matrix2
                    productMatrix[i][j] = productMatrix[i][j] + matrix1[i][k] * matrix2[k][j];
                }
            }
        }
 
        
 
                  System.out.println("\nFirst matrix is : ");
                  for (int i = 0; i < rowsMatrix1; i++) {
                        for (int j = 0; j < columnsMatrix1RowsMatrix2; j++) {
                               System.out.print(matrix1[i][j] + " ");
                        }
                        System.out.println();
                  }
 
                  System.out.println("\nSecond matrix is : ");
                  for (int i = 0; i < columnsMatrix1RowsMatrix2; i++) {
                        for (int j = 0; j < columnsMatrix2; j++) {
                               System.out.print(matrix2[i][j] + " ");
                        }
                        System.out.println();
                  }
 
                  
        System.out.println("\nProduct of matrix1 and matrix2 is");
        for (int i = 0; i < rowsMatrix1; i++) {
            for (int j = 0; j < columnsMatrix2; j++) {
                System.out.print(productMatrix[i][j] + " ");
            }
            System.out.println();
        }
    }
    
}

Kết quả khi chạy chương trình:

Enter number of rows in first matrix : 
3
Enter number of columns in first matrix / rows in matrix2: 
3
Enter number of columns in second matrix : 
3
Enter the first matrix in elements :
1
2
3
4
5
6
7
8
9
Enter the second matrix elements:
9
8
7
6
5
4
3
2
1

First matrix is : 
1 2 3 
4 5 6 
7 8 9 

Second matrix is : 
9 8 7 
6 5 4 
3 2 1 

Product of matrix1 and matrix2 is
30 24 18 
84 69 54 
138 114 90 

4. Chuyển vị ma trận

Xem lưu đồ sau Chuyển vị  ma trận sau:

Mã nguồn chuyển vị ma trận:

//transpose matrix
           int transpose[][] = new int[columns][rows];
           for (int i = 0; i < rows; i++) {
                  for (int j = 0; j < columns; j++)
                        transpose[j][i] = matrix[i][j];
           }

Toàn bộ mã nguồn chương trình chuyển vị ma trận:


import java.util.Scanner;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author ADMIN
 */
public class TransposeMatrix {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
       Scanner scanner = new Scanner(System.in);
       System.out.println("Enter number of rows in matrix : ");
       int rows = scanner.nextInt();
       System.out.println("Enter number of columns in matrix : ");
       int columns = scanner.nextInt();

       int matrix[][] = new int[rows][columns];

       System.out.println("Enter the elements in matrix :");
       for (int i = 0; i < rows; i++) {
              for (int j = 0; j < columns; j++) {
                    matrix[i][j] = scanner.nextInt();
              }
       }

       //transpose matrix
       int transpose[][] = new int[columns][rows];
       for (int i = 0; i < rows; i++) {
              for (int j = 0; j < columns; j++)
                    transpose[j][i] = matrix[i][j];
       }

       System.out.println("\nEntered Matrix is : ");
       for (int i = 0; i < rows; i++) {
              for (int j = 0; j < columns; j++) {
                    System.out.print(matrix[i][j] + " ");
              }
            System.out.println();
       }


       System.out.println("\nTranspose of entered matrix is : ");
       for (int i = 0; i < columns; i++) {
              for (int j = 0; j < rows; j++)
                    System.out.print(transpose[i][j] + " ");

              System.out.println();
       }
    }
    
}

kết quả chuyển vị ma trận:

Enter number of rows in matrix : 
3
Enter number of columns in matrix : 
3
Enter the elements in matrix :
1
2
3
4
5
6
7
8
9

Entered Matrix is : 
1 2 3 
4 5 6 
7 8 9 

Transpose of entered matrix is : 
1 4 7 
2 5 8 
3 6 9