CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2005
    Posts
    2

    Lightbulb Multiplying two matrices...

    I need to multiply two N x N Matrices... Ive seen some sample code on some websites, but they are too complex..any sugestons on a which is the best way to do it?.. any samples?

  2. #2
    Join Date
    Mar 2005
    Posts
    23

    Re: Multiplying two matrices...

    Go to http://mathworld.wolfram.com/MatrixMultiplication.html. In there you will find an algorithm for multiplying any two matrices that can be multiplied.

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Multiplying two matrices...

    If you know to multiply 2 matrices mathematically, it shouldn't be too hard to put in in C/C++.

    Code:
    #define N1 ...
    #define N2 ...
    #define M ...
    
    int A[N1][M], B[M][N2], C[N1][N2];
    for(int i=0; i<N1; i++)
    {
     for(int j=0; j<N2; j++)
     {
       C[i][j] = 0;
       for(k=0; k<M; k++)
         C[i][j] += A[i][k]*B[k][J];
     }
    }
    Very simple...
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    May 2004
    Posts
    340

    Re: Multiplying two matrices...

    yeah i agree with cilu mostly
    but i prefer this way if i had got the chance:
    Code:
    const int M...
    const int N...
    const int MN...
    regards,
    jolley

  5. #5
    Join Date
    Dec 2004
    Posts
    57

    Re: Multiplying two matrices...

    If you don't feel like writing your own matrix classes, you might try TNT:

    http://math.nist.gov/tnt/

  6. #6
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Multiplying two matrices...

    Quote Originally Posted by jolley
    but i prefer this way if i had got the chance:
    I was trying to show how to multiply two matrices, not how to declare their sizes.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  7. #7
    Join Date
    Mar 2005
    Posts
    2

    Smile Re: Multiplying two matrices...

    oh..thanks for the links and thanks for the code guys!...I was looking for a simple code since the one I was writning was too complicated ..but now I get the idea ...I'lll be adding some more code to that to make it work the way I need... then I'll convert it to MIPS assembly...(thats why im looking for a simple code) once again.. thanks for you help guys! If you get any other ideas, please post.. thank you!.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured