# Multiplying Matrices // Nasty For Loops

Page 1 of 1

## 5 Replies - 3198 Views - Last Post: 16 October 2008 - 06:15 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=67712&amp;s=7c0a4d6f4050fa01c0efe86e8f26c998&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Sonastylol

Reputation: 1
• Posts: 151
• Joined: 15-December 07

# Multiplying Matrices // Nasty For Loops

Posted 15 October 2008 - 08:21 AM

Hey guys, Hello Martyr2!

I am trying to put the finishing touches on my Matrix project, but my calculation for multiplying matrices appears to be wrong.

Here is my code first of all, then I will explain:
```MATRIX MATRIX::MultMat(MATRIX MatB)
{
//create a result matrix
MATRIX result;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
result.Mat[i][j] = 0;
for (int k = 0; k < 3; k++) {
result.Mat[i][j] = result.Mat[i][j] + Mat[i][k]*MatB.Mat[k][j];
}
}
}
```

When multiplying Matrices, we take the sum of the multiplication of the first row's cells and the first column's cells.

An example of this would be:

[1 2] [5 6] = [19 22]
[3 4] [7 8] = [43 50]

To find the first # of matrix 3, we do:
(1*5) + (2*7) = 19
Next value (to the right of 19) would be:
(1*6) + (2*8) = 22
Completing the Result Matrix:
(3*5) + (4*7) = 43
(3*6) + (4*8) = 50

I hope I'm doing this correctly.

It involves a bunch of nasty for loops, and I THOUGHT I had it but I don't appear to be correct.

Thank you so much!

Is This A Good Question/Topic? 0

## Replies To: Multiplying Matrices // Nasty For Loops

### #2 Sonastylol

Reputation: 1
• Posts: 151
• Joined: 15-December 07

## Re: Multiplying Matrices // Nasty For Loops

Posted 15 October 2008 - 08:26 AM

Here is my code for working matrix multiplication.

```MATRIX MATRIX::MultMat(MATRIX MatB)
{
//create a result matrix
MATRIX result;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
result.Mat[i][j] = 0;
for (int k = 0; k < 3; k++) {
result.Mat[i][j] = result.Mat[i][j] + Mat[i][k]*MatB.Mat[k][j];
}
}
}

//return new result matrix
return result;
}
```

Now I have to divide Matrices. How do I do this?

This post has been edited by Sonastylol: 15 October 2008 - 12:07 PM

### #3 Sonastylol

Reputation: 1
• Posts: 151
• Joined: 15-December 07

## Re: Multiplying Matrices // Nasty For Loops

Posted 16 October 2008 - 10:39 AM

Bump.

I need to know the formula for dividing matrices.

Thanks!

### #4 Gloin

• Expert Schmexpert...

Reputation: 235
• Posts: 4,489
• Joined: 04-August 08

## Re: Multiplying Matrices // Nasty For Loops

Posted 16 October 2008 - 12:37 PM

Matrix division in general is not so simple but if you're only about to define matrix division for a 2x2 matrix there are ways to simplify the problem.

If you have Matrices A and B and wish to calculate Matrix C = A/B = A x Inverse of B (which is also a 2x2 matrix).
You already have a way to solve AxB so all you need is a way to calculate the inverse of B.

Which is done as follows:
Let B be the matrix:

[a b]
[c d]

Calculate e = 1 / ((a * d) - (b * c))

(ad-bc) is the determinant of the matrix and has to be nonzero if the matrix is to have an inverse (it's a good idea to test this in the beginning of your division function).

multiply e by the matrix:

[d -b]
[-c a]

ex:
Let a = 2, b = 1, c = 4, d = 3 gives the following matrix B

[2 1]
[4 3]

ad-bc = 6 - 4 = 2 => e = 1/2

Gives the inverse of B:

[1.5 -0.5]
[-2.0 1.0]

The rest is just to perform matrix Multiplication with matrix A and then you have you result matrix C.

I hope this wasn't too complicated.

This post has been edited by Gloin: 16 October 2008 - 12:43 PM

### #5 Sonastylol

Reputation: 1
• Posts: 151
• Joined: 15-December 07

## Re: Multiplying Matrices // Nasty For Loops

Posted 16 October 2008 - 01:44 PM

oh wow, seems pretty complicated.

well my matrices are 3x3, and what about if they were even larger than this?

Seems awful.

I havent been able to find any source code of the matter. darn

### #6 Gloin

• Expert Schmexpert...

Reputation: 235
• Posts: 4,489
• Joined: 04-August 08

## Re: Multiplying Matrices // Nasty For Loops

Posted 16 October 2008 - 06:15 PM

Are you really supposed to perform division of one matrix by another or are you only supposed to do division by some constant like A/k??

If you have to do Matrix division of 3x3 matrices then you'll have to do Gauss-elimination (to find the inverse). It's a bit more difficult but definitely doable. Althogh if you haven't seen it before, I guess you're in trouble..

This post has been edited by Gloin: 16 October 2008 - 06:17 PM