Computer >> 컴퓨터 >  >> 프로그램 작성 >> Java

행렬의 전치를 찾는 Java 프로그램

<시간/>

이 기사에서는 행렬의 전치를 찾는 방법을 이해할 것입니다. 행렬에는 요소의 행과 열 배열이 있습니다. m개의 행과 n개의 열로 구성된 행렬을 m × n 행렬이라고 할 수 있습니다. 행렬의 전치는 행을 열로 또는 열을 행으로 교환하여 찾습니다.

아래는 동일한 데모입니다 -

입력이 다음과 같다고 가정 -

The matrix is defined as:
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4

원하는 출력은 -

The transpose of the matrix is:
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4

알고리즘

Step 1 - START
Step 2 - Declare two integer matrices namely input_matrix and result_matrix.
Step 3 - Define the values.
Step 4 - Iterate over each element of the matrix using two for-loops, assign the element at [i][j] position of the matrix to the [j][i] position of the result_matrix.
Step 5 - Display the result_matrix.
Step 6 - Stop

예시 1

여기에서 모든 작업을 'main' 기능 아래에 묶습니다.

public class MatrixTranspose {
   static final int matrix_size = 4;
   public static void main (String[] args) {
      int input_matrix[][] = {
         {1, 1, 1, 1},
         {2, 2, 2, 2},
         {3, 3, 3, 3},
         {4, 4, 4, 4}
      };
      System.out.println("The matrix is defined as: \n");
      for (int i = 0; i < matrix_size; i++) {
         for (int j = 0; j < matrix_size; j++) {
            System.out.print(input_matrix[i][j] + " ");
         }
         System.out.println();
      }
      int result_matrix[][] = new int[matrix_size][matrix_size];
      for (int i = 0; i < matrix_size; i++)
         for (int j = 0; j < matrix_size; j++)
            result_matrix[i][j] = input_matrix[j][i];
      System.out.println("\nThe transpose of the matrix is: ");
      for (int i = 0; i < matrix_size; i++) {
         for (int j = 0; j < matrix_size; j++) {
            System.out.print(result_matrix[i][j] + " ");
         }
         System.out.println();
      }
   }
}

출력

The matrix is defined as:

1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4

The transpose of the matrix is:
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4

예시 2

여기에서 객체 지향 프로그래밍을 나타내는 함수로 작업을 캡슐화합니다.

public class MatrixTranspose {
   static final int matrix_size = 4;
   static void transpose(int input_matrix[][]) {
      int result_matrix[][] = new int[matrix_size][matrix_size];
      for (int i = 0; i < matrix_size; i++)
         for (int j = 0; j < matrix_size; j++)
            result_matrix[i][j] = input_matrix[j][i];
      System.out.println("\nThe transpose of the matrix is: ");
      for (int i = 0; i < matrix_size; i++) {
         for (int j = 0; j < matrix_size; j++) {
            System.out.print(result_matrix[i][j] + " ");
         }
         System.out.println();
      }
   }
   public static void main (String[] args) {
      int input_matrix[][] = {
         {1, 1, 1, 1},
         {2, 2, 2, 2},
         {3, 3, 3, 3},
         {4, 4, 4, 4}
      };

      System.out.println("The matrix is defined as: \n");
      for (int i = 0; i < matrix_size; i++) {
         for (int j = 0; j < matrix_size; j++) {
            System.out.print(input_matrix[i][j] + " ");
         }
         System.out.println();
      }
      transpose(input_matrix);
   }
}

출력

The matrix is defined as:

1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4

The transpose of the matrix is:
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4