Matrix operations (transpose)

I want to perform matrix operation (for example find the transpose matrix of a given matrix A)

I found some libraries to do so, for example Colt:

In the second link is mentioned that if you want to print the transpose, you type:


However, I don't want to print the transposed matrix. I want to store it in a second matrix, for example B, that would have the appropriate dimensions.

Any ideas?


Have you seen the library Apache Commons Math? e.g.:

public static void main(String[] args) {
    double[][] data = { { 1, 2, 3 }, { 4, 5, 6 } };

    RealMatrix matrix = MatrixUtils.createRealMatrix(data);

    RealMatrix transpose = matrix.transpose();

Here is another example of transposing matrices in la4j (Linear Algebra for Java):

// create simple 2D array matrix of double[][] array
Matrix a = new Basic2DMatrix(new double[] {
  { 1.0, 2.0, 3.0 },
  { 4.0, 5.0, 6.0 },
  { 7.0, 8.0, 9.0 }

// transpose out-of-place
Matrix b = a.transpose();

// output matrices  

With colt library, you can simply do

DoubleMatrix2D transposed = matrix.viewDice();

viewDice() method returns a DoubleMatrix2D.

Just to add my 2 cents because I was thinking of implementing a matrix transpose by myself.

Could we add an wrapper to the original java 2D array?

When you transpose a matrix, you don't transpose the actual array at all, instead simply flag it as transpose=true in the wrapper.

When you want to access value at (3,2) via the wrapper, the wrapper swaps the row and column indices and return the value at (2,3)

You can do the same to other operations until an actual transpose is "really" necessary.

By "really" I mean the actual transposed matrix has computation performance advantages than using the original matrix, which mainly depends on what operations you will perform on the matrix.

Need Your Help

Create dmg installer with $HOME/Application folder

macos installer packagemaker dmg home-directory

I have created script for application installation into root Application folder (/Application), but I'm not able to create the same one for user home folder $HOME/Application (~/Application).

Trouble parsing quotes with SAX parser (javax.xml.parsers.SAXParser) on Android API 1.5

android saxparser

When using a SAX parser, parsing fails when there is a " in the node content. How can I resolve this? Do I need to convert all " characters?