# Matrices and Multiplication

• 01-07-2002
SyntaxBubble
Matrices and Multiplication
I am reading the NeHe tutorial in Vectors and Matrices. In 7th grade, my math teacher there is no way to multiply matrices with diff. columns and rows. I found different on the tutorial. Can someone explain exactly how to do this?

In the tutorial:

| 1 3 | | 1 3 5 |
M = | 2 4 | X = | 2 4 6 |

M*X= | 7 15 23 |
| 10 22 34 |

The tutorial says:

Now, lets get on with doing the actual matrix multiplication! To do this, you multiply each row of the first matrix by
each column of the second matrix.

Can anyone explain how to get M*X?
• 01-07-2002
VirtualAce
In doing 3D, each matrix should be the same size so this does not matter.

Code:

```void MatrixMultiply(double matrix1[4][4],double matrix2[4][4],double result[4][4]) {   for (int i=0;i<4;i++)   {     for (int j=0;j<4;j++)     {         result[i][j]=0;         for (int k=0;k<4;k++)         {           result[i][j]+=(matrix1[i][k]*matrix2[k][j]);         }     }   } }```
If you want it to be a bit faster concatenating then unroll the second loop.

Code:

```void MatrixMultiply(double matrix1[4][4],double matrix2[4][4],double result[4][4]) {   for (int i=0;i<4;i++)   {     for (int j=0;j<4;j++)     {         result[i][j]=((matrix1[i][0]*matrix2[0][j])           + (matrix1[i][1]*matrix2[1][j])           + (matrix1[i][2]*matrix2[2][j])           + (matrix1[i][3]*matrix2[3][j]));             }   } }```
Unrolling the first or second loop will not give you any significant speed gains.

I'm not sure exactly what OpenGL does with its matrices or how it concatenates them. Seems that every program does it a bit different than the next.

AFAIK, you cannot multiply matrices of different sizes - you cannot correctly concatenate matrices of unlike sizes. In using matrices your vertexes must be in the form x,y,z,t and your final screen coords will be x,y,t. The t is just so that the math comes out right and should always be 1. The only way I know that you could try to concatenate diff sizes is to change the i and j values in the for loop, but it will still not come out right mathematically.

Perhaps there was a misprint on the page.
• 01-07-2002
taylorguitarman
Matrix multiplication is this:
any matrix A of size m x n can only be multiplied by matrix B if B is an n x k matrix. The resulting matrix will be an m x k matrix. The matrix form is rows x cols. It is possible to do multiplication of different sized matrices as long as this condition is met.
So a 3x3 matrix can be multiplied by a 3x1 matrix to result in a 3x1 matrix. Notice that matrix multiplication is not necessarily associative.
Many useful things can be derived from matrix manipulation. You can learn more about it in linear algebra.
A hint- you can do systems of equations very quickly once you know some of these concepts, instead of doing it the long and hard way like you'll be taught in you math classes. I always hated how they made you do it the hard way for a while and then showed you the easy way. Walk before you can run I guess.
• 01-07-2002
taylorguitarman
Oh, and Bubba's right; in game programming, you'll most likely dealing with 4x4 matrices. And most of your graphics APIs will have the functions to do the matrix operations for you so you won't really have to worry much about it.