Thread: Pruduct of matrixes

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    30

    Pruduct of matrixes

    Hello, all:

    I need to calculate the product of two known matrixes A(1xn) and B(nxn). n is about 30 to 50:

    C=A*B

    How calculate the C(1xn) with a program?

  2. #2
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    http://mathworld.wolfram.com/MatrixMultiplication.html

    Here is a quick although, not complete example...

    Code:
    /*================================================
     A program which performs matrix 
     multiplication.
     
     Notes
     -----
     Multiplication can only take place iff
     the number of columns in matrix A is equal 
     to the number of rows in matrix B.
     
     Or as I like to remember it:
         
           C A R B
           
     Columns in
     A must equal
     Rows in
     B
     
     ================================================*/
    
    #include <iostream>
    
    using namespace std;
    
    
    //Function declarations
    void mmult(int,int,int[][100],int[][100]);
    
    int main()
    {
        //Hard code matrix A
        int m1[100][100] = {{1,5},
                            {2,7},
                            {3,4}};
        
        //Hard code matrix B
        int m2[100][100] = {{8,4,3,1},
                            {2,5,8,6}};
        //call function
        mmult(3,4,m1,m2);
        
        //Where '3' is the maximum number of rows in either
        //matrix A or B
        //and '4' is the maximun number of columns in either
        //matrix A or B
        
        cin.get();
        cin.get();             
         
       return 0;
    }    
    /*=================================================  =====
      Function to multiply matrix
     ==================================================  ===*/                  
    void mmult (int rows, int cols, int m1[][100], int m2[][100]) 
    {
        int m3[100][100];//holds the result of A * B
        for (int i=0; i<rows; i++) 
        {
            for (int j=0; j<cols; j++) 
            {
              int val = 0;
              for (int k=0; k<cols; k++) 
              {
                val = val + m1[i][k] * m2[k][j];
              }
              m3[i][j] = val;
            }
        }
        //print matrix
        for (int i=0; i<rows; i++) 
        {
            for (int j=0; j<cols; j++) 
            {
              cout<<" "<<m3[i][j];
    
              
            }
            cout<<"\n";
        }
    }
    Last edited by treenef; 01-04-2006 at 04:00 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Books for C matrixes
    By pedofil in forum C Programming
    Replies: 1
    Last Post: 04-17-2008, 04:24 AM
  2. Help with calculations with tables ( matrixes )
    By sdherzo in forum C Programming
    Replies: 11
    Last Post: 06-20-2007, 07:01 AM
  3. multiply 2 matrixes
    By BianConiglio in forum C Programming
    Replies: 2
    Last Post: 04-14-2005, 01:52 AM
  4. OpenGL: Depth Testing, Matrixes, ect.?
    By Zeusbwr in forum Game Programming
    Replies: 8
    Last Post: 12-08-2004, 09:37 AM
  5. matrixes for collision detection
    By DavidP in forum Game Programming
    Replies: 10
    Last Post: 11-09-2002, 10:31 PM