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.
Code Java:
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;
}
}
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.
Re: Adding two matrix together
How do I test to see if the matrices are the same size.
Re: Adding two matrix together
Try this and see if it makes sense.
Code Java:
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
}
}
Re: Adding two matrix together
Do you know how I could write a matrix multiplication method
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
Code java:
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.
Re: Adding two matrix together
could you help me with writing the loops to do the calculations.