# Matrix multiplication (works with 2 but not with more)

This topic is 4831 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm making a 3D game using 3D rotation matrixes, and multiplying them together to add different effects. I've documented myself on this subject on google and made a function to mutiply matrices, etc. it works efficiently when using 2 matrices, but when using more than one i multiply like this (AxB)xC and all i get is my objet screwed up all ways on screen. How must i multiply more than 2 matrices together? thanks ;) i've not found any website mentionning those here's the method i'm using, just in case matrix format: double matrix[] 0, 1, 2, 3, 4, 5, 6, 7, 8

void mulMatrix(double* MatrixA, double* MatrixB, double* dest_matrix){
dest_matrix[0] = MatrixA[0] * MatrixB[0] + MatrixA[1] * MatrixB[3] + MatrixA[2] * MatrixB[6];
dest_matrix[1] = MatrixA[0] * MatrixB[1] + MatrixA[1] * MatrixB[4] + MatrixA[2] * MatrixB[7];
dest_matrix[2] = MatrixA[0] * MatrixB[2] + MatrixA[1] * MatrixB[5] + MatrixA[2] * MatrixB[8];

dest_matrix[3] = MatrixA[3] * MatrixB[3] + MatrixA[4] * MatrixB[3] + MatrixA[5] * MatrixB[6];
dest_matrix[4] = MatrixA[3] * MatrixB[4] + MatrixA[4] * MatrixB[4] + MatrixA[5] * MatrixB[7];
dest_matrix[5] = MatrixA[3] * MatrixB[5] + MatrixA[4] * MatrixB[5] + MatrixA[5] * MatrixB[8];

dest_matrix[6] = MatrixA[6] * MatrixB[6] + MatrixA[7] * MatrixB[3] + MatrixA[8] * MatrixB[6];
dest_matrix[7] = MatrixA[6] * MatrixB[7] + MatrixA[7] * MatrixB[4] + MatrixA[8] * MatrixB[7];
dest_matrix[8] = MatrixA[6] * MatrixB[8] + MatrixA[7] * MatrixB[5] + MatrixA[8] * MatrixB[8];
}



##### Share on other sites
Matrix multiplication is associative. It shouldn't matter if you multiply A and B together first or B and C together first.

##### Share on other sites
> Matrix multiplication is associative. It shouldn't matter if you multiply A and B together first or B and C together first.

i know, but it doesn't work...
i do something like:

C= AxB
E= CxD

and i use E, however it just manages to screw up my object instead of doing nice rotations like each matrix does separately

##### Share on other sites
Quote:
 Original post by Marianne dest_matrix[3] = MatrixA[3] * MatrixB[3] + MatrixA[4] * MatrixB[3] + MatrixA[5] * MatrixB[6]; dest_matrix[4] = MatrixA[3] * MatrixB[4] + MatrixA[4] * MatrixB[4] + MatrixA[5] * MatrixB[7]; dest_matrix[5] = MatrixA[3] * MatrixB[5] + MatrixA[4] * MatrixB[5] + MatrixA[5] * MatrixB[8];

should be
    dest_matrix[3] = MatrixA[3] * MatrixB[0] + MatrixA[4] * MatrixB[3] + MatrixA[5] * MatrixB[6];    dest_matrix[4] = MatrixA[3] * MatrixB[1] + MatrixA[4] * MatrixB[4] + MatrixA[5] * MatrixB[7];    dest_matrix[5] = MatrixA[3] * MatrixB[2] + MatrixA[4] * MatrixB[5] + MatrixA[5] * MatrixB[8];
and
Quote:
 Original post by Marianne dest_matrix[6] = MatrixA[6] * MatrixB[6] + MatrixA[7] * MatrixB[3] + MatrixA[8] * MatrixB[6]; dest_matrix[7] = MatrixA[6] * MatrixB[7] + MatrixA[7] * MatrixB[4] + MatrixA[8] * MatrixB[7]; dest_matrix[8] = MatrixA[6] * MatrixB[8] + MatrixA[7] * MatrixB[5] + MatrixA[8] * MatrixB[8];

should be
    dest_matrix[6] = MatrixA[6] * MatrixB[0] + MatrixA[7] * MatrixB[3] + MatrixA[8] * MatrixB[6];    dest_matrix[7] = MatrixA[6] * MatrixB[1] + MatrixA[7] * MatrixB[4] + MatrixA[8] * MatrixB[7];    dest_matrix[8] = MatrixA[6] * MatrixB[2] + MatrixA[7] * MatrixB[5] + MatrixA[8] * MatrixB[8];

##### Share on other sites
thanks! it works great!

the site i've been on must have been wrong (i don't reckon i made an error cause it did work for simple mul)

##### Share on other sites
quote:
>> Matrix multiplication is associative. It shouldn't matter if you multiply A and B together first or B and C together first.

>i know, but it doesn't work...

You are both wrong. And I think you mean commutative instead of associative.
AND matrix multiplication are in general NOT commutative so it does matter whether you multiply like this AxB or this BxA. A clear example of this is the multiplaication of a 2x3 matrix with a 3x2 matrix... One way around it gives a 2x2 matrix. The other way around, a 3x3 matrix comes out as result..

##### Share on other sites
For multiplication:

Associative means (a*b)*c = a*(b*c)

Commutative means a*b = b*a

so Marianne and SiCrane were correct in using the term associative, and correct in saying that it applies to matrix multiplication. Matrix multiplication is not commutative, but then that was never an issue...

##### Share on other sites
Yes I know what associative and communicative means. Reading the post again I see I missread it. I see he was actually talking about the associative rule..=)
Geez..
My mistake - sorry!

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

• 13
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633328
• Total Posts
3011380
• ### Who's Online (See full list)

There are no registered users currently online

×