7.25 LAB: Flip MatrixWrite a function that flips the values of an n x n matrix along its diagonal. The diagonal is defined as the elements with the same index on both axes (i.e., 0,0 or 1,1 and so on). Elements that lie on the diagonal are not changed while other elements are swapped with the opposite position (i.e., element in [1][3] switched with [3][1]). Ensure that a valid matrix is provided.Ex: If the input is:16 3 2 135 10 11 89 6 7 124 15 14 1the output is:16 5 9 4 3 10 6 15 2 11 7 14 13 8 12 1 Hint: Use nested loops to iterate through the matrix and swap the appropriate elements.
Question
7.25 LAB: Flip MatrixWrite a function that flips the values of an n x n matrix along its diagonal. The diagonal is defined as the elements with the same index on both axes (i.e., 0,0 or 1,1 and so on). Elements that lie on the diagonal are not changed while other elements are swapped with the opposite position (i.e., element in [1][3] switched with [3][1]). Ensure that a valid matrix is provided.Ex: If the input is:16 3 2 135 10 11 89 6 7 124 15 14 1the output is:16 5 9 4 3 10 6 15 2 11 7 14 13 8 12 1 Hint: Use nested loops to iterate through the matrix and swap the appropriate elements.
Solution
Here is a Python solution for the problem:
def flip_matrix(matrix):
n = len(matrix)
for i in range(n):
for j in range(i+1, n):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
return matrix
# Test the function
matrix = [[16, 3, 2, 13], [5, 10, 11, 8], [9, 6, 7, 12], [4, 15, 14, 1]]
print(flip_matrix(matrix))
This function works by iterating over each row of the matrix with the outer loop (for i in range(n)). For each row, it then iterates over each column that is to the right of the diagonal (for j in range(i+1, n)). It then swaps the element at position [i][j] with the element at position [j][i].
The result is a matrix where the elements have been flipped along the diagonal.
Similar Questions
You are given an N x M binary matrix called "image". You need to perform the following operations on the matrix (in order) and return the resulting image:Flip the image horizontally: This involves reversing the order of elements in each row of the matrix. For example, [1,0,1,0,0,0] becomes [0,0,0,1,0,1]Invert the image: This involves replacing 0s with 1s and 1s with 0s in the entire matrix. For example, [0,0,0,1,0,1] becomes [1,1,1,0,1,0]Input FormatLine of input contains N - number of rows and M - number of columns. The next N lines contains M integers each denoting the elements of the matrix image.Output FormatYou have to print the resultant matrix image.
Given an n x n binary matrix image, flip the image horizontally, then invert it, and return the resulting image.To flip an image horizontally means that each row of the image is reversed.For example, flipping [1,1,0] horizontally results in [0,1,1].To invert an image means that each 0 is replaced by 1, and each 1 is replaced by 0.For example, inverting [0,1,1] results in [1,0,0].
#include <iostream>#include <vector>void rotateMatrix(int** matrix, int n) { // Transpose the matrix for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { std::swap(matrix[i][j], matrix[j][i]); } } // Reverse each row for (int i = 0; i < n; i++) { int left = 0; int right = n - 1; while (left < right) { std::swap(matrix[i][left], matrix[i][right]); left++; right--; } }}void printMatrix(int** matrix, int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { std::cout << matrix[i][j] << ' '; } std::cout << std::endl; }}int main() { int n; std::cin >> n; if (n <= 0) { std::cout << "Size of the matrix should be positive!" << std::endl; return 0; } // Allocate dynamic memory for the matrix int** matrix = new int*[n]; for (int i = 0; i < n; i++) { matrix[i] = new int[n]; } // Read the matrix elements for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { std::cin >> matrix[i][j]; } } std::cout << "Original Matrix:" << std::endl; printMatrix(matrix, n); rotateMatrix(matrix, n); std::cout << "Matrix after 90-degree clockwise rotation:" << std::endl; printMatrix(matrix, n); // Deallocate the dynamic memory for (int i = 0; i < n; i++) { delete[] matrix[i]; } delete[] matrix; return 0;}
You've got a 5 × 5 matrix, consisting of 24 zeroes and a single number one. Let's index the matrix rows by numbers from 1 to 5 from top to bottom, let's index the matrix columns by numbers from 1 to 5 from left to right. In one move, you are allowed to apply one of the two following transformations to the matrix:Swap two neighboring matrix rows, that is, rows with indexes i and i + 1 for some integer i (1 ≤ i < 5).Swap two neighboring matrix columns, that is, columns with indexes j and j + 1 for some integer j (1 ≤ j < 5).You think that a matrix looks beautiful, if the single number one of the matrix is located in its middle (in the cell that is on the intersection of the third row and the third column). Count the minimum number of moves needed to make the matrix beautiful.InputThe input consists of five lines, each line contains five integers: the j-th integer in the i-th line of the input represents the element of the matrix that is located on the intersection of the i-th row and the j-th column. It is guaranteed that the matrix consists of 24 zeroes and a single number one.OutputPrint a single integer — the minimum number of moves needed to make the matrix beautiful.ExamplesinputCopy0 0 0 0 00 0 0 0 10 0 0 0 00 0 0 0 00 0 0 0 0outputCopy3inputCopy0 0 0 0 00 0 0 0 00 1 0 0 00 0 0 0 00 0 0 0 0outputCopy1
If a column vector A=[1 2 3 4], then fliplr(A) returnsa.a column vector [1 2 3 4]b.a row vector [1 2 4 3]c.a row vector [4 3 2 1]d.a column vector [4 3 2 1]
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.