# Transposing a matrix

• 12-05-2010, 03:25 AM
atomant
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.

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, 03:31 AM
atomant
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.
• 12-05-2010, 03:34 AM
Fubarable
Quote:

Originally Posted by atomant
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.
• 12-05-2010, 03:51 AM
al_Marshy_1981
rows and columns have they come out of thin air?
• 12-05-2010, 03:53 AM
atomant
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, 03:54 AM
atomant
Quote:

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

?

Code:

```int rows = matrix.length; int columns = matrix[0].length;```
• 12-05-2010, 04:00 AM
Fubarable
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, 04:26 AM
al_Marshy_1981
sorry, it has been a long day for me, you are right of course. sorry again
• 12-05-2010, 04:32 AM
al_Marshy_1981
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, 06:31 AM
pbrockway2
Quote:

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, 08:49 AM
atomant
Quote:

Originally Posted by al_Marshy_1981
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
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-05-2010, 09:10 AM
JosAH
Quote:

Originally Posted by atomant
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:

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