0
votes

Assume I have a matrix A.What do I have to type to get the transposed matrix of A? (lets say B)

(I have imported Apache Commons Math in my project and I want to do it using these libraries)

My code is:

double[][] A = new double[2][2];
        A[0][0] = 1.5;
        A[0][1] = -2.0;
        A[1][0] = 7.3;
        A[1][1] = -13.5;

Then,what?...

(I have found this link, but I don't know what exactly to do:

http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/linear/RealMatrix.html

I have tried :

double[][] a = new double[2][2];
a = RealMatrix.transpose();

Also,

double[][] a = new double[2][2];
a = A.transpose();

And how can I transpose an array a in the same way?

2
I wrote above my tries :) - Konstantinos Mama-Sita
@vels4j I have seen this, but I want to use the subroutine from the Commons Math library. And I want to do so, because I inntend to use even more subroutines of that library... - Konstantinos Mama-Sita
I mean that I don't want to create a new subroutine, I want to use the one from the library. - Konstantinos Mama-Sita
Transpose is a method called on an instance of RealMatrix, it is not a static method. You need to create a RealMatrix first then set the rows and columns (.setRow and .setColumn methods) and then you can transpose it. - arynaq

2 Answers

1
votes
double[][] matrixData = { {1.5d,2d}, {7.3d,-13.5d}};
RealMatrix m = MatrixUtils.createRealMatrix(matrixData);

There is a method called transpose that returns the transpose of the matrix

RealMatrix m1=m.transpose();

m1 would be the transpose of m

1
votes

You can try something like:

public void transpose() {

        final int[][] original = new int[][] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
        for (int i = 0; i < original.length; i++) {
            for (int j = 0; j < original[i].length; j++) {
                System.out.print(original[i][j] + " ");
            }
            System.out.print("\n");
        }
        System.out.print("\n\n matrix transpose:\n");
        // transpose
        if (original.length > 0) {
            for (int i = 0; i < original[0].length; i++) {
                for (int j = 0; j < original.length; j++) {
                    System.out.print(original[j][i] + " ");
                }
                System.out.print("\n");
            }
        }
    }