# Multiplying Matrices Confusion

Discussion in 'Mac Programming' started by Caster, Oct 14, 2007.

1. ### Caster macrumors newbie

Joined:
Jul 18, 2007
#1
I hope the topic name doesn't scare away too many people, because I'm looking for help, once again lol.

I've been working on a program to multiply two matrices (two dimensional arrays). I think I've got most of it down, but the problem is, I can't figure out how to store the result in the 3rd array.
ie
C[1][1] = (B[1][1] * A[1][1]) + (B[1][1] * A[1][2]) + (B[1][1] * A[1][3])
(if thats right, I've become so confused, and I know it starts at 0,0 but starting off at 1 helps me remember lol).

So here is my source code:
Code:
```import java.util.Scanner;

public class MulitplyingMatrices {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the width of matrix a");
int matrixA_rows = scanner.nextInt();
System.out.println("Please enter the height of matrix a");
int matrixA_cols = scanner.nextInt();
System.out.println("Please enter the width of matrix b");
int matrixB_rows = scanner.nextInt();
System.out.println("Please enter the height of matrix b");
int matrixB_cols = scanner.nextInt();

/*
if(matrixA_rows != matrixB_cols)
exit();
*/

int[][] matrixA = new int[matrixA_rows][matrixA_cols];
int[][] matrixB = new int[matrixB_rows][matrixB_cols];
int[][] matrixC = new int[matrixB_rows][matrixA_cols];
GenerateArray(matrixA);
GenerateArray(matrixB);
MultiplyArrays(matrixA, matrixB, matrixC);

}

public static void GenerateArray(int[][] matrix) {
for(int i = 0; i < matrix.length; i++)
{
for(int j = 0; j < matrix[i].length; j++ )
{
matrix[i][j] = 1 + (int)(Math.random() * 10);
// System.out.println("Matrix[" + i + "][" + j + "] - " + matrix[i][j]);
}
}
}

public static void MultiplyArrays(int[][] matrixA, int[][] matrixB, int[][] matrixC) {
int temp = 0;
int counter = 1;
int temp2 = 0;
for(int i = 0; i < matrixB.length; i++)
{
for(int j =  0; j < matrixA[i].length; j++)
{
temp += matrixB[i][j] * matrixA[j][i];
// System.out.println(temp);
}

temp2 = counter - 1;
matrixC[temp2][i] = temp;
System.out.println("MatrixC[" + temp2 + "][" + i + "] - " + matrixC[temp2][i]);
if(counter == matrixB.length)
counter = 1;
else
counter++;
}

}
}

```
After reading the thread of why offering help, I feel sort of guilty to ask, but I feel accomplished that I've made it this far, but I don't expect someone to write the code for me, so if you'd rather just generally explain it (without a code), or explain how to keep track and store the result in matrixC, I would appreciate that just as much--I just feel stuck and confused lol.

Thanks for all the help!

2. ### garethlewis2 macrumors 6502

Joined:
Dec 6, 2006
#2
I will give you a very little hint.

You are missing a for loop in your matrix multiply.

The summation notation will have something like

c[ik][jk] = axb

The k is the third for loop.

Just search for matrix multiply in google. There are thousands of sites with this coded in C++, C, Java, Basic

3. ### Caster thread starter macrumors newbie

Joined:
Jul 18, 2007
#3
When you say C[ik][[jk] do u mean multiply i * j, j * k? or what? I'm still trying to figure this out.

4. ### jstad macrumors regular

Joined:
Jun 13, 2007
#4
I am in the same problem. I think I know what you are talking about but need to know what your doing (math matically makes sense) just need to know how you are trying to store it now?

5. ### garethlewis2 macrumors 6502

Joined:
Dec 6, 2006
#5
No it is not a scalar product but the position referenced by those two loop counters.

Here is a version written in C

void Multiply3DMatrices(Matx4x4 A, Matx4x4 B, Matx4x4 C) {
int i, j, k;
float ab;

for(i = 0; i < 4; i++)
{
for(j = 0; j < 4; j++)
{
ab=0;
for(k = 0; k < 4; k++) ab = A[k]*B[k][j]
C[j] = ab;
}
}
}

Matx4x4 is a float two dim array