I'm new to java and am trying to find the transpose of a matrix X with m rows and x columns. I'm not familiar with java syntax but I think I can access the number of rows using double[x.length] but how do I figure out the number of columns? Here is my code:
import stdlib.StdArrayIO;
public class Transpose {
// Entry point
public static void main(String[] args) {
double[][] x = StdArrayIO.readDouble2D();
StdArrayIO.print(transpose(x));
}
// Returns a new matrix that is the transpose of x.
private static double[][] transpose(double[][] x) {
// Create a new 2D matrix t (for transpose) with dimensions n x m, where m x n are the
// dimensions of x.
double[][] t = new double[x.length][x[1.length]]
// For each 0 <= i < m and 0 <= j < n, set t[j][i] to x[i][j].
// Return t.
}
}
Also, I was trying to figure it out by printing a 2D list in a scrap file but It wouldn't work for some reason, where is what I had:
import stdlib.StdArrayIO;
import stdlib.StdOut;
public class scrap {
public static void main(String[] args){
double[][] x = new double[10][20];
StdOut.println(x.length);
}
}
Could you point out what I was doing wrong? It kept waiting on input. I could also use any other tips that would help, I am transitioning from Python.
x[1.length]->x[1].length