Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Below is my function to transpose a Matrix in Java which has the form double [][]:

public static double[][] transposeMatrix(double [][] m){

    for (int i = 0; i < m.length; i++) {

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

            double temp = m[i][j];

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

            m[j][i] = temp;

        }

    }

    return m;

}

But it’s not working properly. Can anyone tell me what I’m doing wrong here?

1 Answer

0 votes
by (19.7k points)

Check the code below:

 public static double[][] transposeMatrix(double [][] m){

        double[][] temp = new double[m[0].length][m.length];

        for (int i = 0; i < m.length; i++)

            for (int j = 0; j < m[0].length; j++)

                temp[j][i] = m[i][j];

        return temp;

    }

Interested in Java? Check out this Java tutorial by Intellipaat.

Related questions

0 votes
1 answer
asked Sep 26, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Oct 3, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Jul 29, 2019 in Python by Rajesh Malhotra (19.9k points)
0 votes
2 answers
asked Jul 6, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Jan 24, 2021 in Java by dante07 (13.1k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...