Matrix Multiplication

Write code for matrix multiplication.

Answer:



(M x N) * (N x P) = (M x P) matrix
^^^^^^^

N
---->
1 2 3 x 1 4 |
4 5 6 2 5 | N
3 6 \/

From the above diagram, it can be infered that
M and P will form the outer loops and N will be the
innermost loop.

for ( i = 0 to M )
for ( j = 0 to P )
{
a[i][j] = 0;
for ( k = 0 to N )
{
a[i][j] += a[i][k] * a[k][j];

}
}




No comments: