stryke404
stryke404

Reputation: 27

Matrix Transpose in Java

I have a method transpose in class Matice. When i apply this method on my m2.matice i can see during debugging that my matrix is being transposed, but when i print m2 i get the same matrix as before.

    public class Main {
    public static void main(String[] args) {
        Matice m1 = new Matice(5);
        m1.matrixFill(0, 5);
        m1.matrixPrint();

        Matice m2 = new Matice(3);
        m2.matrixFill(-5, 10);
        m2.matrixPrint();

        m2.transpose();
        m2.matrixPrint();
    }
}

package matice;
/**
 *
 * @author Adam Rychter
 */
public class Matice {

    int[][] matice;
    private int n;

    public Matice(int n) {
        this.n = n;
        if(n > 0) {
            matice = new int[n][n];
        }
    }

    public void matrixPrint(){

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.format("%5d", matice[i][j]);                
            }
            System.out.println("");
        }
        System.out.println("");
    }

    public void matrixFill(int a, int b){
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                matice[i][j] = (int) (Math.random() * (a + b + 1) - a);                
            }
        }
    }

    public void transpose(){
        int tmp;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                tmp = matice[i][j];
                matice[i][j] = matice[j][i];
                matice[j][i] = tmp;                
            }            
        }
    }
}

Upvotes: 0

Views: 2071

Answers (4)

chandra
chandra

Reputation: 41

public void transpose(){
    int tmp;
    for (int i = 0; i < n; i++) {
        for (int j = i+1; j < n; j++) {
            tmp = matice[i][j];
            matice[i][j] = matice[j][i];
            matice[j][i] = tmp;                
        }            
    }
}

Upvotes: 0

DSlomer64
DSlomer64

Reputation: 4283

Like this, maybe:

    public void transpose(){
        Matice mL = new Matice(n);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                mL.matice[i][j] = matice[j][i];
            }
        }
        matice = mL.matice;
   }

See alternate, more efficient answer above.

Upvotes: 0

DSlomer64
DSlomer64

Reputation: 4283

Note that j goes from i to n, not 1 to n; also note need for two tmp variables.

  public void transpose(){

    int tmp1, tmp2;
    for (int i = 0; i < n; i++) {
        for (int j = i; j < n; j++) {
            tmp1 = matice[i][j];
            tmp2 = matice[j][i];
            matice[i][j] = tmp2;
            matice[j][i] = tmp1;
        }
    }

Upvotes: 1

okaram
okaram

Reputation: 1542

Your problem is that you're transposing each element twice ! once when i=row, j=col, and once when i=col, j=row, with the net effect of leaving the matrix as it was. The easiest way I see to fix it is do the swap only if i>j (and then test to make sure :) this is a suggestion, not working code :)

Upvotes: 3

Related Questions