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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Adding two matrix together

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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;
    	}
     
    }
    Last edited by helloworld922; February 28th, 2011 at 11:25 PM.


  2. #2
    Junior Member
    Join Date
    Feb 2011
    Posts
    4
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default 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. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Adding two matrix together

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

  4. #4
    Junior Member
    Join Date
    Feb 2011
    Posts
    4
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default 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. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Adding two matrix together

    Do you know how I could write a matrix multiplication method

  7. #6
    Junior Member
    Join Date
    Feb 2011
    Posts
    4
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default 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. #7
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Adding two matrix together

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

Similar Threads

  1. Matrix Program
    By panzer in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 30th, 2013, 06:15 AM
  2. Having problem in matrix multiplication....
    By sidhant in forum Java Theory & Questions
    Replies: 5
    Last Post: March 17th, 2011, 01:41 PM
  3. Issue Building Graph in Adjacency Matrix
    By mike.s in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 22nd, 2010, 02:44 PM
  4. Embedding images in a matrix.
    By Aims_ in forum Java Theory & Questions
    Replies: 1
    Last Post: September 11th, 2009, 02:37 PM
  5. Creating A Count Matrix In Java
    By statsman5 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: July 14th, 2009, 04:40 PM