In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.