Matrix Transpose and matrix multiplication

Source: Internet
Author: User
Preface

The reason for writing this blog is to record the implementation code for matrix transpose and matrix multiplication, for future contingencies. The direct reason is that tonight (2016.09.13) participated in the Baidu 2017 school recruit's written test (C + + post), there is a matrix transpose after the online programming problem. In view of the future written examination may be used, hereby record, but also hope to help the needs of netizens.

Tonight's Baidu written test there is a way to find the number of squares in a rectangular box, you can use similar to finding a feasible path in the Maze Search (DFS) plus backtracking method to solve, fortunately before the maze problem and recorded written down as a blog, otherwise, and tragic, a short period of time it is difficult to write so many code. 1. Transpose The Matrix 1.1 Introduction to transpose matrices

A new matrix, called a transpose matrix (transpose of a matrix), is used to replace the row of matrix A with the column of the same ordinal, and is recorded at A^t.

For example:

Therefore, the characteristics of the transpose matrix:
(1) The number of rows of a transpose matrix is the number of columns of the original matrix, and the number of columns of the transpose matrix is the number of rows of the original matrix;
(2) The elements of the transpose matrix subscript (I,J) correspond to the elements of the original matrix subscript (j,i). 1.2 Implementation

Using the two-dimensional array as the storage structure of the matrix, it is easy to get the transpose matrix according to the characteristics of transpose matrix.

/**************************************************
* @para: Matrix: Original matrix; row: Number of rows of matrix; column: Number of matrix columns
* @ret: return transpose matrix
**************************************************/
int** gettransposematrix (int** matrix,int Row, int column) {
   int** matrixr=new int*[columns];
   for (int i=0;i<columns;++i) {
        matrixr[i]=new int[rows];
   }

   for (int i=0;i<row;++i) {for
        (int j=0;j<column;++j) {
            matrixr[j][i]=matrix[i][j];
        }
   }
   return MATRIXR;
}
2. Multiplication of matrices 2.1 Introduction to matrix multiplication

Set a to the matrix of the MXP m\times p,B is the matrix of PXN p\times N, then the matrix C of the MXN m\times n is matrix A and b is denoted as a c=ab, where the column I of line I of the matrix C element can be represented as:

Examples are as follows:

Characteristics of matrix multiplication:
(1) A and B can be multiplied when the number of columns of matrix A equals the number of rows in matrix B.
(2) The element of the nth column of the product C is equal to the sum of the elements of line m of matrix A and the corresponding elements of the nth column of Matrix B.
(3) The number of rows in Matrix C equals the number of rows in matrix A, and the number of columns in C equals the number of columns in B. 2.2 Sample Code

/********************************************
* @para: A: Matrix A; B: Matrix b;c: Multiply result matrix; rowa:a number of rows; columnb:b number of columns ; columna:a number of columns
* @ret: void 
********************************************/
void Matrixmul (int **a, int **b , int **c, int rowa, int columnb, int columnA) {for
    (int i=0;i<rowa;i++) {for
        (int j=0; j<columnb;j++) {
            C[I][J] = 0;
            for (int k=0;k<columna;k++) {
                c[i][j]+=a[i][k]*b[k][j];}}}}
Reference Documents

[1] Transpose matrix Baidu Encyclopedia
[2] matrix multiplication Baidu Encyclopedia

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.