Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the cycle of transpose Matrix in C++

Shulou Source: shulou.com Published: 2022-06-02 04:33:57 10月02日 Update

This article mainly introduces the C++ how to achieve the cycle of the transpose matrix, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

Preface

The transpose of the matrix mainly examines our use of the loop, through the simple loop structure, we can easily complete the transpose of the matrix.

First, train of thought analysis

The difference between the transpose matrix and the original matrix lies in the column exchange. We can construct a two-dimensional array to complete the storage of the original matrix. We only need to exchange each element in the opposite position of the row and column to complete the transposition of the matrix.

Second, the code implementation 1. Transpose matrix function

We first write a function to complete the transpose of the matrix.

The code is as follows (example):

/ * Alkaid#3529 * / / transfer matrix function that transforms the matrix to void transpose_matrix (int matrix [10] [10], int a) according to the specified matrix size; / / the function implements void transpose_matrix (int matrix [10] [10], int a) / / int matrix [10] [10] [10] as the array of storage matrices, and int an is the size of the desired transpose matrix {int mid = 0 / / Intermediate quantity, passing for of auxiliary values (int I = 0; I < a; iSum +) / / starting from the line, retrieve {for line by line (int j = I + 1; j < a) Matrix +) / / A pair of elements in each column of the row to retrieve {/ / swap the two elements in the corresponding position mid = matrix [I] [j]; matrix [I] [j] = matrix [j] [I]; matrix [j] [I] = mid }} return;} 2. Call the function to realize the transpose matrix

We only need to call the written function in the main function and output the matrix before and after transpose.

The code is as follows (example):

/ * Alkaid#3529 * / # includeusing namespace std;// transfer matrix function to transfer the matrix to void transpose_matrix (int matrix [10] [10], int a) according to the specified matrix size; int main () {int matrix [10] [10]; int size = 0; for (int I = 0; I < 10; iMatrix +) {for (int j = 0; j < 10) Matrix +) {matrix [I] [j] = (I * I + j * I + j ^ I)% 10;}} / / read the required matrix size, for convenience, use the default value of the matrix, which defaults to square matrix cout size; cout

Tags: Matrix function element size location jobless loop article retrieval code array storage output Category two just example column transfer auxiliary Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Redmi Linux MariaDB vpn