Matrix Multiplication

abhi's Avatar, Join Date: Aug 2008
Newbie Member
pls let me know the logic for multiplying a matrix...i have been racking my brains on that
0
aortizb's Avatar, Join Date: Sep 2008
Go4Expert Member
please could you elaborate your question?
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Multiplying by a constant or multiplying two matrices?
If the former then it's just a case of multiplying each element by that number. a{p,q,r} = {ap,aq,ar}.
Multiplying two matrices is more involved, first you must check that the number of columns in the first equals the number of rows in the second, then:
[a0 a1] [b0 b1 b2] = [a0b0+a1b3 a0b1+a1b4 a0b2+a1b5 ]
[a2 a3] [b3 b4 b5] [a2b0+a3b3 a2b1+a2b4 a2b2+a3b5 ]

Is it the maths you're stuck on or the implementation?
0
abhi's Avatar, Join Date: Aug 2008
Newbie Member
i want the source code not the math behind it.... how to implement it using the concepts of loops and arrays only?
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
It's not difficult, where are you stuck? If I give you two matrices, e.g.
[ 1 2 ] [ 5 6 7 ]
[ 3 4 ] [ 8 9 0 ]

how far do you get with "write a program to multiply them"? Do you at least get a main function?
Obviously you'll need some way to represent the matrices, you're correct to use arrays, that's exactly what they're designed for, and you're not going to be able to do this generically without some kind of loop.
Have you figured out how to store the matrices in the arrays yet?
0
abhi's Avatar, Join Date: Aug 2008
Newbie Member
yes i do,
i have taken the two input matrix matrix declared as a[3][3] b[3][3] using loop
n use 3 for loops,one inside other
i,j,k,initiated thm at zero, & i<=2,j<=2, k<=0
n used
C[i][j]=c[i][j]+a[i][j]*b[j][k]
is it correct?
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Could you post the actual code rather than a description? I don't see the point of what appears from the description to be for (k=0; k<=0; k++). From that I'd say no for "is it correct", but presumably you either get the right or wong answer, if you get the wrong answer then it's clearly not correct.