h-s
h-s

Reputation: 43

How to working with matrices using java

i have come up with a code to calculate the transpose of a matrix, but it seems to giving an error... any advice regarding improving the code would be helpful.

public class Prob1_Matrices {

    public static int[][] Transpose2D(int m[][]) {
       int B[][] = new int[m.length][m[0].length];

        for (int i = 0; i < m.length; i++) {
            for (int j = 0; j < m[0].length; j++) {
                int temp = B[i][j];
                B[i][j] = B[j][i]; // this is line 10
                B[j][i] = temp;
            }
        }
        return B;
    }

    public static void main(String[] args) {
        int m[][] = {{2, 5, 8, 3, 6},
        {4, 2, 9, 3, 1},
        {8, 0, 9, 5, 2}};
        System.out.println("Matrix:");
        System.out.println(Transpose2D(m)); //this is line 22
    }

}

Upvotes: 0

Views: 2019

Answers (4)

jhamon
jhamon

Reputation: 3691

B is the transpose Matrix, so maybe it should have the opposite size of m?

As m dimension are:

m.length * m[0].length

B should be:

m[0].length * m.length

a | b | c

d | e | f

shall be transpose to

a | d

b | e

c | f

Other problem is you don't use m in your function. a correct use should be:

    for (int i = 0; i < m[0].length; i++) {
        for (int j = 0; j < m.length; j++) {
            B[i][j] = m[j][i];
        }
    }

edit: swap i and j limit. thanks to David Wallace

Upvotes: 1

damgad
damgad

Reputation: 1446

There are three problem in your code.

Firstly you get an ArrayIndexOutOfBoundsException because here:

B[i][j] = B[j][i];

you are trying to access non existing index in your array B.

Size of B should be declared as int B[][] = new int[m[0].length][m.length];as you want B matrix to be a transpose of m.


Secondly, you don't use the m array when you calculating B array. I also don't see why you use a temporary variable int temp, that should do what you need:

B[j][i] = m[i][j];


The third problem is that you here: System.out.println(Transpose2D(m)); you are trying to print array as it is. That will not work. Try this:

System.out.println(Arrays.deepToString(Transpose2D(m)))

Upvotes: 3

3yakuya
3yakuya

Reputation: 2662

B[i][j] = B[j][i];
B[j][i] = temp;

If your matrix is not square, this will possibly throw ArrayIndexOutOfBoundsException (if your array is for example 2x10 and you try array[1][8] = array[8][1].) You probably meant it more like:

public static int[][] Transpose2D(int m[][]) {
   int B[][] = new int[m[0].length][m.length];

    for (int i = 0; i < m.length; i++) {
        for (int j = 0; j < m[0].length; j++) {
            B[j][i] = m[i][j];
        }
    }
    return B;
}

Upvotes: 0

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79808

You have three problems - the first is that you've declared the wrong dimensions for B. It should be new int[m[0].length][m.length], not the other way around.

The second is that you are trying to copy entries from B back to itself, before creating any data for B. So you're actually copying nothing. The third problem - even if you had entries in B, you're swapping each pair of them twice, so this code would never have any effect.

You want to copy entries from m to B instead. So in the loop, you want B[j][i] = m[i][j]; instead of trying to swap entries within B.

Upvotes: 0

Related Questions