在Java中转置矩阵

huangapple go评论69阅读模式
英文:

Transposing matrices in Java

问题

我觉得我在Java方面缺少一些基本的东西,我不确定为什么我的下面的代码不起作用,我的步骤:

  1. 输入是一个2x2的矩阵,
  2. 复制原始矩阵,
  3. 循环遍历行,然后列,
  4. 将原始矩阵的列值赋给转置矩阵的行。
static void transpose(int[][] matrix) {
    int[][] temp = matrix.clone();
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            matrix[i][j] = temp[j][i];
        }
    }
}
英文:

I think I am missing something fundamental here about Java, I am not sure why my code below do not work, my steps:

  1. the input is a 2x2 matrix,
  2. copy the original matrix,
  3. loop through rows then column,
  4. assign original matrix's column values to the rows of the transpose matrix.
static void transpose(int[][] matrix) {
    int[][] temp = matrix.clone();
    for (int i = 0; i &lt; 2; i++) {
        for (int j = 0; j &lt; 2; j++) {
            matrix[i][j] = temp[j][i];
        }
    }
}

答案1

得分: 0

问题是使用函数clone(),因为它会创建一个仅共享行数组的新矩阵。

使用相同的代码风格,这段代码有效:

int[][] temp = new int[2][2];
for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
        temp[i][j] = matrix[j][i];
    }
}

注意,由于temp是一个空矩阵,比较将是temp = matrix

因此,这适用于简单的2x2矩阵,但如果你需要其他维度,而不是

int[][] temp = new int[2][2];

使用

int[][] temp = new int[matrix[0].length][matrix.length];

顺便说一下,更新矩阵的引用内存不是一个好的做法。我认为你的方法应该返回temp矩阵。

一个完整的更新,一个更好的方法可能是这样的。它几乎和你的方法一样,但可以接受不同的长度。

public static int[][] transpose(int [][] matrix){
    int[][] temp = new int[matrix[0].length][matrix.length];
    for (int i = 0; i < matrix.length; i++) {
        for (int j = 0; j < matrix[i].length; j++) {
	        temp[j][i] = matrix[i][j];
        }
    }
    return temp;
}
英文:

The problem is use the function clone() because that will create a new matrix sharing only row arrays.

Using the same code style, that works:

int[][] temp = new int[2][2];
for (int i = 0; i &lt; 2; i++) {
    for (int j = 0; j &lt; 2; j++) {
        temp[i][j] = matrix[j][i];
    }
}

Note that, as temp is an empty matrix, the comparsion will be temp = matrix.

So this works for a simple 2x2 matrix, but if you need other dimensions, instead of

int[][] temp = new int[2][2];

Use

int[][] temp = new int[matrix[0].length][matrix.length];

By the way, update the reference memory of the matrix is not a good practice. I think your method should return temp matrix.

An entire update, a better method could be this one. Is almost the same you have but it can accept diferent lengths.

public static int[][] transpose(int [][] matrix){
    int[][] temp = new int[matrix[0].length][matrix.length];
    for (int i = 0; i &lt; matrix.length; i++) {
        for (int j = 0; j &lt; matrix[i].length; j++) {
	        temp[j][i] = matrix[i][j];
        }
    }
    return temp;
}

答案2

得分: 0

你可以使用IntStream来替代for循环

public static void main(String[] args) {
    int[][] m1 = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}};

    int[][] m2 = transpose(m1);

    Arrays.stream(m2).map(Arrays::toString).forEach(System.out::println);
    // [1, 4, 7]
    // [2, 5, 8]
    // [3, 6, 9]
}
static int[][] transpose(int[][] matrix) {
    return IntStream.range(0, matrix.length).mapToObj(i ->
            IntStream.range(0, matrix[i].length).map(j ->
                    matrix[j][i]).toArray())
            .toArray(int[][]::new);
}
英文:

You can use IntStream instead of for loop:

public static void main(String[] args) {
    int[][] m1 = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}};

    int[][] m2 = transpose(m1);

    Arrays.stream(m2).map(Arrays::toString).forEach(System.out::println);
    // [1, 4, 7]
    // [2, 5, 8]
    // [3, 6, 9]
}
static int[][] transpose(int[][] matrix) {
    return IntStream.range(0, matrix.length).mapToObj(i -&gt;
            IntStream.range(0, matrix[i].length).map(j -&gt;
                    matrix[j][i]).toArray())
            .toArray(int[][]::new);
}

huangapple
  • 本文由 发表于 2020年10月18日 03:58:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/64406758.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定