The realization of the two N*N matrix multiplication, matrix is represented by a

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

The realization of the two N*N matrix multiplication, matrix is represented by a one-dimensional array.

First introduce the matrix addition:

复制代码

1     void Add(int rows, int cols)  
2     {  
3        for(int i= 0;i<rows;i++)  
4        {  
5        for(int j=0;j<cols;j++)  
6           result[i][j]=mat1[i][j]+mat2[i][j];  
7        }  
8     } 

复制代码

If the two matrix to do multiplication: only in the number of columns in a matrix of rows and the other a matrix of the same , can do two matrix multiplication.

How to get the matrix transpose:

The transpose of the matrix is a matrix, change in the original matrix transpose matrix column for. For example, the following a 3× 3 matrix;:

1 2 3

6 7 8

4 5 9

Then the transpose it into:

1 6 4

2 7 5

3 8 9

Let us from the programmer's point of view carefully examine this phenomenon. Assuming that the original array is M, transposed matrix is MT. Then M[1][0] = 6, we found that MT in the transposed matrix [0][1] = 6. Therefore, we can get the conclusion: program to transpose a matrix is actually on the index variable. Technical terms:

  1. MT[Row][Column] = M[Column][Row]; 

The following is the transpose of C code:

[cpp] view plaincopy

  1. void show_transpose(float mat[][10],int row,int col)

  2. {

  3. int i,j;

  4. for(i=0;i<row;i++)

  5. {

  6. for(j=0;j<col;j++)

  7. printf("%f\t",mat[j][i]);

  8. printf("\n");

  9. }

  10. }

This method shows the matrix transpose.

[cpp] view plaincopy

    1. #include<iostream>  
    2. using namespace std;  
    3. #define size 2  
    4.   
    5. int multi(int *a , int *b , int N)  
    6. {  
    7. int i , j , k , temp;  

    8. int *c = (int*)malloc(N * N * sizeof(int));  

    9.   
    10. for(i = 0 ; i < N ; i++)  

    11. {  

    12. for(j = 0 ; j < N ; j++)  

    13. {  

    14. temp = i * N + j;  

    15. *(c + temp) = 0;  

    16. for(k = 0 ; k < N ; k++)  

    17. {  

    18. *(c + temp) += a[i * N + k] * b[k * N + j];  

    19. }  

    20. cout<<*(c + temp)<<" ";  

    21. }  

    22. }  

    23. return *c;  

    24. }  
    25.   
    26. int main()  
    27. {  
    28. int a[size * size] = {2 , 1 , 4 , 3};  

    29. int b[size * size] = {1 , -1 , 3 , 2};  

    30. multi(a , b , size);  

    31.   
    32. return 0;  


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Hunter at November 16, 2013 - 2:08 PM