Results 1 to 12 of 12
  1. #1
    atomant is offline Member
    Join Date
    May 2010
    Posts
    45
    Rep Power
    0

    Default Transposing a matrix

    Does this code look okay? I am trying to transpose a matrix, make the rows of matrix 1 the columns of matrix 2. I'm having trouble with eclipse and I can't get this to work, so I don't know if the problem is just eclipse or the code too.

    Java Code:
    import acm.program.*;
    
    public class transpose extends Program {
    	
    	public void run() {
    		int[][] mat = new int[2][5];
    		mat[0][0] = 1;
    		mat[0][1] = 2;
    		mat[0][2] = 3;
    		mat[0][3] = 4;
    		mat[0][4] = 5;
    		mat[1][0] = 6;
    		mat[1][1] = 7;
    		mat[1][2] = 8;
    		mat[1][3] = 9;
    		mat[1][4] = 10;
    		println(mat);
    		//println(transpose(mat));
    		
    		
    	}
    
    	public transpose(int matrix[][]) {
    		// Get size
    		int rows = matrix.length; 
    		int columns = matrix[0].length; 
    		
    		int[][] newMatrix = new int [rows][columns];
    		
    		for (int i = 0; i < rows; i++) {
    			for (int j = 0; i < columns; i++) {
    				newMatrix[i][j] = matrix[j][i];
    			}
    		}
    		println(newMatrix);
    		
    	}
    }

  2. #2
    atomant is offline Member
    Join Date
    May 2010
    Posts
    45
    Rep Power
    0

    Default

    I just noticed that the rows and columns should be switched on one line.

    int[][] newMatrix = new int [columns][rows];

    but it still doesn't work.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by atomant View Post
    Does this code look okay? I am trying to transpose a matrix, make the rows of matrix 1 the columns of matrix 2. I'm having trouble with eclipse and I can't get this to work, so I don't know if the problem is just eclipse or the code too.
    So, what do you think that the odds are that you've made a mistake in your code, or that the authors of Eclipse have made an error, ... in other words that you're smarter than them. I'll give you a hint, it's a very very very low number.

    You'll want to tell us what the error message is and what line of code calls it.

  4. #4
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    rows and columns have they come out of thin air?

  5. #5
    atomant is offline Member
    Join Date
    May 2010
    Posts
    45
    Rep Power
    0

    Default

    What part of "I'm having trouble with eclipse and I can't get this to work" implies that it's the authors of eclipse fault and not my own? I obviously didn't mean that the authors of eclipse made the mistake... I meant that I was either making a mistake in eclipse or in my code.

    If you see my other thread, I am having trouble with everything running as an applet instead of an application. When I run it as an applet I get "load: transpose.class can't be instantiated.java.lang.InstantiationException: transpose". When I try to run it as an application I get "Exception in thread "main" acm.util.ErrorException: Cannot determine the main class." At one time when I was trying to run it as an application it threw an exception at line 40,
    newMatrix[i][j] = matrix[j][i];

  6. #6
    atomant is offline Member
    Join Date
    May 2010
    Posts
    45
    Rep Power
    0

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    rows and columns have they come out of thin air?
    ?

    Java Code:
    int rows = matrix.length; 
    int columns = matrix[0].length;

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Looks like a problem with the ACM library. Do you need to re-install this? I don't use it and am not familiar with it as it is not part of standard Java.

  8. #8
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    sorry, it has been a long day for me, you are right of course. sorry again

  9. #9
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Not sure if your a student or anything but I have yet to see the benefit of teaching the ACM library i really dont, and I know stanford teach it, I really do not get why, maybe somebody has an answer to this.

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    What part of "I'm having trouble with eclipse and I can't get this to work" implies that it's the authors of eclipse fault and not my own?

    No part of it does.

    Your original post made the assertion "I don't know if the problem is just eclipse or the code too". Without wishing to cause you offense, I think we have good reason to ignore the possibility that the problem lies with eclipse and concentrate instead on your code. Indeed if we take the "too" at the end seriously then by far and away the most likely state of affairs is that the problem lies not in "just eclipse" nor in eclipse and "the code too": but rather in "just the code".

    -------------------

    Is "public transpose(int matrix[][])" supposed to be a construtor? If so, where does it get invoked and with what argument.

    I agree with the suggestion given earlier: we need to see the code and the compiler message or runtime exception or whatever it is.

  11. #11
    atomant is offline Member
    Join Date
    May 2010
    Posts
    45
    Rep Power
    0

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    Not sure if your a student or anything but I have yet to see the benefit of teaching the ACM library i really dont, and I know stanford teach it, I really do not get why, maybe somebody has an answer to this.
    You nailed it, I'm taking the Standford online class. I'm not sure why they teach it, as I never see it used in anything else.

    Quote Originally Posted by pbrockway2 View Post
    No part of it does.
    Is "public transpose(int matrix[][])" supposed to be a construtor? If so, where does it get invoked and with what argument.

    I agree with the suggestion given earlier: we need to see the code and the compiler message or runtime exception or whatever it is.
    I just noticed that it's not being called in run() anymore since I've commented out that line. So running this should work just fine, since all it does is println a matrix. Like I said in my last post I was getting an exception on line 40, but now I can't even get that far. There is a problem with how I have eclipse set up. I tried creating a whole new project from scratch and now when I try to run it I get this "java.lang.NoSuchMethodError: main
    Exception in thread "main"". I get this after right clicking the class, Run Config, and trying to run it as Java Project with transpose as the name of the main. I'm not sure what else to try.

    I just installed the Android sdk and I've also been copying the project between my pc and laptop via sd card. So I guess I screwed something up when doing one of these?

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by atomant View Post
    I just noticed that the rows and columns should be switched on one line.

    int[][] newMatrix = new int [columns][rows];

    but it still doesn't work.
    You should keep your definitions clear: suppose you access a matrix like this: matrix[row][co], i.e. the first index value indexes a row and the second one indexes a column. Indeed if 'transpose' is the transposed of matrix 'matrix' then transpose[row][col] == matrix[col][row]. That is the central notion to the construction of your transposed matrix:

    Java Code:
    int[][] getTransposed(int[][] matrix) {
       int[][] transpose= new int[matrix[0].length][matrix.length];
       for (int row= 0; row < transpose.length; row++)
          for (int col= 0; col < transpose[row].length; col++)
             transpose[row][col]= matrix[col][row];
       return transpose;
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Help with dox matrix printer
    By Albert in forum Advanced Java
    Replies: 7
    Last Post: 09-06-2011, 09:50 AM
  2. reading Matrix
    By metrokid in forum New To Java
    Replies: 1
    Last Post: 12-04-2010, 01:48 PM
  3. Transposing on a Digraph in Java
    By Growler in forum New To Java
    Replies: 0
    Last Post: 11-14-2010, 11:29 PM
  4. help in matrix
    By Engineer in forum New To Java
    Replies: 7
    Last Post: 10-06-2010, 02:26 PM
  5. Help with matrix
    By susan in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •