Javatpoint Logo
Javatpoint Logo

Check Matrix Transformation by Flipping Sub-Matrices along the Principal or Anti-Diagonal

Matrix transformation, also called matrix operation or matrix manipulation, refers to the use of explicit tasks to a matrix, bringing about an adjusted or changed matrix. Matrices are mathematical designs that comprise of a variety of numbers coordinated in rows and columns.

Matrix Transformations includes many of the operations such as Scalar multiplication, Matrix addition and subtraction, Transpose, Matrix flipping and many more.

Problem Statement:

You are given a square matrix of size N x N, where every cell contains an integer (whole number). Your task is to carry out two sorts of matrix transformations: flipping along the principal diagonal and flipping along the anti-diagonal.

For this above given statement we can use the matrix transformation - Matrix flipping. It will reverse the order of the elements of the matrix in selected submatrices based on specific patterns.

The Principal Diagonal Flip:

The principle diagonal flip includes switching the order for elements along the principal diagonal of a matrix. This diagonal runs from the upper left to the base right. The change is accomplished by swapping elements across this diagonal.

Approach:

Traverse the matrix along the principal diagonal, i.e., for every element at position (i, j), swap it with the element at position (j, i).

Example:

Matrix = [[9, 8, 5],[3, 2, 1],[6, 5, 4]]

Output: After performing the principal-diagonal flip the obtained output is [[9, 3, 6],[8, 2, 5],[4, 1, 4]].

Anti-Diagonal Flip:

The anti-diagonal flip includes switching the order for elements along the anti-diagonal of a matrix of a matrix. This diagonal runs from the upper right to the base left. The change is accomplished by swapping elements across this anti-diagonal.

Approach:

Cross the matrix along the anti-diagonal, i.e., for every element at position (i, j), swap it with the element at position (n-j-1, n-i-1), where n is the size of the matrix.

Example:

Matrix = [[9, 8, 5],[3, 2, 1],[6, 5, 4]]

Output: After performing the anti-diagonal flip the obtained output is [[4, 8, 9],[1, 2, 3],[4, 5, 6]].

Code Implementation (Java):


Check Matrix Transformation by Flipping Sub-Matrices along the Principal or Anti-Diagonal

Explanation:

  1. This code imports inbuilt libraries for manipulating array and list.
  2. It specifies the MatrixTransformation class.
  3. The principalDiagonalFlip function, takes a 2D structure as info and returns another 2D structure (result) after playing the principal diagonal flip.
  4. rs and cs have the line and segment number in the primary lattice.
  5. So, you get one more new matrix whose elements are sorted in the inverted order.
  6. Nested loops underlie the single unit of the initial grid.
  7. Render qualities (i.e. swapping row(line) and column(segment) indices) and keep them in result lattice.
  8. The result is that the flipped framework gets returned.
  9. antiDiagonalFlip is a strategy/process of anti-diagonal flip equivalent to principalDiagonalFlip as well.
  10. Loop statements are used to iterate through each element of the main matrix.
  11. The values are shifted into the result matrix along the rows and columns indicated by rs and cs.
  12. The transpose of the given matrix (input) is returned.
  13. printMatrix is function that prints a lattice (matrix) components along with the given title.
  14. 3x3 matrix (originalMatrix) is defined by the main method.
  15. Prints out the first matrix. Calls the principalDiagonalFlip function, adds the flipped matrix.
  16. That function calls the antiDiagonalFlip function, and the printed matrix is the flipped one.

Conclusion:

An elementary problem is flipping along the principal or anti-diagonal, among the computed matrix transformations.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA




news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news