Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Matrix multiplication

Status
Not open for further replies.
ok the down and dirty of what you need to do is

AxB=C
A B C=3x3 matrix
[2 3] [2 3 4] [c11 c12 c13]
[3 4] X [3 4 5] = [c21 c22 c23]
[4 5] [c31 c32 c33]

c11 = 2*2 + 3*3 = 13
c12 = 2*3 + 3*4 = 18
c13 = 2*4 + 3*5 = 23
c21 = 3*2 + 3*3 = 21

etc...

What I suggest is you research matrix multiplication. It is not too difficult once you get the hang of it. Matrix A's Cols must = the Matrix B's rows to do a cross product. You multiply a11 with b11 and add it to a12 x b21 where the first number a11 represents row and the second number a11 represents column.

Good Luck :)

Matt
 
Sorry, to do it with C++ you would have arrays

Code:
int matrixA[3][2];
int matrixB[2][3];
int matrixC[3][3];

memset(matrixC,0,sizeof(int)*3*3);


int col = 0;
int row = 0;

for(int i = 0; i<3;i++)
{
    for(int j = 0;j<2;j++)
    {
       matrixC[col][row]+= matrix[i][j];
       col++;
       if(col == 3)
       {
             col = 0;
             row++;
       }
    }
}

sorry for the mis-interpretation, I got side tracked and thought you were asking how to do matrix multiplicaiton.

Matt
 
MAN... 2 for 2... sorry bout not putting in the code tag once again... in need more coffee

I think this should do the trick


Sorry, to do it with C++ you would have arrays

Code:
int matrixA[3][2];
int matrixB[2][3];
int matrixC[3][3];

memset(matrixC,0,sizeof(int)*3*3);


int col = 0;
int row = 0;

for(int i = 0; i<3;i++)
{
    for(int j = 0;j<2;j++)
    {
       matrixC[col][row]+= matrix[i][j];
       col++;
       if(col == 3)
       {
             col = 0;
             row++;
       }
    }
}

sorry for the mis-interpretation, I got side tracked and thought you were asking how to do matrix multiplicaiton.

Matt


 
man.. i have NOT had enough coffee... i am sorry. What I said above is invalid.

Please dis-regard it. As you said you do need 4 for loops

for(int acol = 0;acol<2;acol++)
{
for(int arow = 0;arow<3;arow++)
{
for(int bcol = 0;bcol < 3;bcol++)
{
for(int brow = 0;brow<2;brow++)
{
matrixC[row][col] = matrixA[acol][arow]*matrixB[bcol][brow];

col++;
if(col==3)
{
col = 0;
row++;
}
}
}
}
}

I THINK THIS IS THE ANSWER but with my track record this morning I dont know

PLEASE TEST IT

Matt


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top