Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## Adding two matrix together

This is all I could come up with and I can't figure out how to get them to add up together.

```public class UtilityMatrix {

public static int[][] addMatrix(int[][] a, int[][] b) {
int[][] c = new int[3][3];

for ( int i = 0; i<a.length; i++){
for( int j = 0; j<a[i].length; j++){

}

return c;
}

}```

2. ## Re: Adding two matrix together

Ted,

You should test to ensure the matrices a and b are the same size. You may only add matrices that are the same size. There's a length method of arrays but I think you'll need to check length of the outer array and each of the inner arrays. Once you take car of all that, move the return c line outside the second for loop. Inside both for loops you need this line.

c[i][j] = a[i][j] + b[i][j];

Along with checking your array szies you could size the c array appropriately instead of making it a static 3 x 3.

3. ## Re: Adding two matrix together

How do I test to see if the matrices are the same size.

4. ## Re: Adding two matrix together

Try this and see if it makes sense.

```public class UtilityMatrix {

public static int[][] addMatrix(int[][] a, int[][] b) {
int[][] bad = {{0}}; // a dummy to send back if the matrices are not the same size
int max_depth = 0; // stores the max length of the sub-arrays, in case a jagged array is passed
// ensure arrays are the same size
if (a.length != b.length) return bad; // empty set
for (int i = 0; i<a.length; i++){
if (a[i].length != b[i].length) return bad; // empty set
if (a[i].length > max_depth) max_depth = a.length;
}
// end tests
int [][] c = new int [a.length][max_depth]; // now we know how big the return array should be.
for ( int i = 0; i<a.length; i++){
for( int j = 0; j<a[i].length; j++){
c[i][j] = a[i][j]+b[i][j]; // here we add the corresponding elements
}
}
return c; // send back the sum array
}
}```

5. ## The Following User Says Thank You to codethrupain For This Useful Post:

papated21 (March 1st, 2011)

6. ## Re: Adding two matrix together

Do you know how I could write a matrix multiplication method

7. ## Re: Adding two matrix together

Remember that matrix multiplication requires that the number of rows in matrix A equal the number of columns in matrix B. The rows are the first dimension of the array and the columns are the second dimension.

Since jagged arrays just plain won't work for multiplication, you can bypass that test and just confirm that the sizes match

`if (a.length != b[1].length) return bad; //sizes don't match`

Next is the calculation. Say the first matrix is a[3][2] and the second is b[2][3]. The result would be matrix c[3][3].
c[1][1] = a[1][1] * b[1][1] + a[1][2] * b[2][1] + a[1][3] * b[3][1]
c[1][2] = a[2][1] * b[1][1] + a[2][2] * b[2][1] + a[2][3] * b[3][1]
...

Note what is repeated (the b matrix items) and what changes each time (the a matrix items) So the "a" matrix would be incremented in the inner loop and the "b" matrix would be incremented in the outer loop.

The tricky part is referencing your arrays correctly using your loop counters. Since you know the result matrix is 3x3, it will take two loops just to address each item in the c matrix and a third loop to increment the keys of the a and b arrays for each calculation of an element in array c.

See what you can do with that.

8. ## The Following User Says Thank You to codethrupain For This Useful Post:

papated21 (March 2nd, 2011)

9. ## Re: Adding two matrix together

could you help me with writing the loops to do the calculations.