Results 1 to 12 of 12
Thread: Transposing a matrix
- 12-05-2010, 02:25 AM #1
Member
- Join Date
- May 2010
- Posts
- 45
- Rep Power
- 0
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); } }
- 12-05-2010, 02:31 AM #2
Member
- Join Date
- May 2010
- Posts
- 45
- Rep Power
- 0
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.
-
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.
- 12-05-2010, 02:51 AM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
rows and columns have they come out of thin air?
- 12-05-2010, 02:53 AM #5
Member
- Join Date
- May 2010
- Posts
- 45
- Rep Power
- 0
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];
- 12-05-2010, 02:54 AM #6
Member
- Join Date
- May 2010
- Posts
- 45
- Rep Power
- 0
-
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.
- 12-05-2010, 03:26 AM #8
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
sorry, it has been a long day for me, you are right of course. sorry again
- 12-05-2010, 03:32 AM #9
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
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.
- 12-05-2010, 05:31 AM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,560
- Rep Power
- 11
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.
- 12-05-2010, 07:49 AM #11
Member
- Join Date
- May 2010
- Posts
- 45
- Rep Power
- 0
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.
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-05-2010, 08:10 AM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,588
- Blog Entries
- 7
- Rep Power
- 17
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:
kind regards,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; }
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Help with dox matrix printer
By Albert in forum Advanced JavaReplies: 7Last Post: 09-06-2011, 08:50 AM -
reading Matrix
By metrokid in forum New To JavaReplies: 1Last Post: 12-04-2010, 12:48 PM -
Transposing on a Digraph in Java
By Growler in forum New To JavaReplies: 0Last Post: 11-14-2010, 10:29 PM -
help in matrix
By Engineer in forum New To JavaReplies: 7Last Post: 10-06-2010, 01:26 PM -
Help with matrix
By susan in forum New To JavaReplies: 1Last Post: 08-07-2007, 04:37 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks