如何交换二维数组的行和列?

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

How to swap rows and columns of a 2d array?

问题

我正在尝试编写一个用于“转置”二维整数数组的方法,其中原矩阵的行和列被交换。

然而,我不知道如何实现这个方法。
我应该如何编写这个方法?

public class Matrix {
    private int[][] numbers;

    public Matrix(int rows, int columns) {
        if (rows < 1)
            rows = 1;
        else
            rows = rows;
        if (columns < 1)
            columns = 1;
        else
            columns = columns;
        numbers = new int[rows][columns];
    }

    public final void setNumbers(int[][] numbers) {
        this.numbers = numbers;
    }

    public int[][] getNumbers() {
        return numbers;
    }

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

I'm trying to write a method for 'transpose' a two-dimensional array of integers, in which the rows and columns of the original matrix are exchanged.

However, I have no idea how I can realize this.
How do I write out this method?

public class Matrix {
    private int[][] numbers;

    public Matrix(int rows, int colums) {
        if (rows &lt; 1)
            rows = 1;
        else
            rows = rows;
        if (colums &lt; 1)
            colums = 1;
        else
            colums = colums;
        numbers = new int[rows][colums];
    }

    public final void setNumbers(int[][] numbers) {
        this.numbers = numbers;
    }

    public int[][] getNumbers() {
        return numbers;
    }

    public int[][] transpose() {
        int[][] transpose = getNumbers();
        return numbers;
    }
}

答案1

得分: 8

你可以遍历行和列,并将每个元素[i,j]赋值给转置的[j,i]:

/**
 * 转置矩阵。
 * 假设:mat是一个非空矩阵,即:
 * 1. mat != null
 * 2. mat.length > 0
 * 3. 对于每个i,mat[i].length相等且mat[i].length > 0
 */
public static int[][] transpose(int[][] mat) {
    int[][] result = new int[mat[0].length][mat.length];
    for (int i = 0; i < mat.length; ++i) {
        for (int j = 0; j < mat[0].length; ++j) {
            result[j][i] = mat[i][j];
        }
    }
    return result;
}
英文:

You could iterate over the rows and columns and assign each element [i,j] to the transposed [j,i]:

/**
 * Transposses a matrix.
 * Assumption: mat is a non-empty matrix. i.e.:
 * 1. mat != null
 * 2. mat.length &gt; 0
 * 3. For every i, mat[i].length are equal and mat[i].length &gt; 0
 */
public static int[][] transpose(int[][] mat) {
    int[][] result = new int[mat[0].length][mat.length];
    for (int i = 0; i &lt; mat.length; ++i) {
        for (int j = 0; j &lt; mat[0].length; ++j) {
            result[j][i] = mat[i][j];
        }
    }
    return result;
}

答案2

得分: 1

矩阵的转置

int m = 4;
int n = 5;
// 原始矩阵
int[][] arr1 = {
    {11, 12, 13, 14, 15},
    {16, 17, 18, 19, 20},
    {21, 22, 23, 24, 25},
    {26, 27, 28, 29, 30}};

// 转置矩阵
int[][] arr2 = new int[n][m];
// 交换行和列
IntStream.range(0, n).forEach(i ->
        IntStream.range(0, m).forEach(j ->
                arr2[i][j] = arr1[j][i]));

// 输出到 Markdown 表格
String matrices = Stream.of(arr1, arr2)
        .map(arr -> Arrays.stream(arr).map(Arrays::toString)
                .collect(Collectors.joining("<br>")))
        .collect(Collectors.joining("</pre> | <pre>"));

System.out.println("| 原始矩阵 | 转置矩阵 |");
System.out.println("|---|---|");
System.out.println("| <pre>" + matrices + "</pre> |");
原始矩阵 转置矩阵
[11, 12, 13, 14, 15]
[16, 17, 18, 19, 20]
[21, 22, 23, 24, 25]
[26, 27, 28, 29, 30]
[11, 16, 21, 26]
[12, 17, 22, 27]
[13, 18, 23, 28]
[14, 19, 24, 29]
[15, 20, 25, 30]

另请参阅:使用数组打印蛇形图案

英文:

The transpose of a matrix

int m = 4;
int n = 5;
// original matrix
int[][] arr1 = {
        {11, 12, 13, 14, 15},
        {16, 17, 18, 19, 20},
        {21, 22, 23, 24, 25},
        {26, 27, 28, 29, 30}};

// transposed matrix
int[][] arr2 = new int[n][m];
// swap rows and columns
IntStream.range(0, n).forEach(i -&gt;
        IntStream.range(0, m).forEach(j -&gt;
                arr2[i][j] = arr1[j][i]));

// output to the markdown table
String matrices = Stream.of(arr1, arr2)
        .map(arr -&gt; Arrays.stream(arr).map(Arrays::toString)
                .collect(Collectors.joining(&quot;&lt;br&gt;&quot;)))
        .collect(Collectors.joining(&quot;&lt;/pre&gt; | &lt;pre&gt;&quot;));

System.out.println(&quot;| original matrix | transposed matrix |&quot;);
System.out.println(&quot;|---|---|&quot;);
System.out.println(&quot;| &lt;pre&gt;&quot; + matrices + &quot;&lt;/pre&gt; |&quot;);
original matrix transposed matrix
<pre>[11, 12, 13, 14, 15]<br>[16, 17, 18, 19, 20]<br>[21, 22, 23, 24, 25]<br>[26, 27, 28, 29, 30]</pre> <pre>[11, 16, 21, 26]<br>[12, 17, 22, 27]<br>[13, 18, 23, 28]<br>[14, 19, 24, 29]<br>[15, 20, 25, 30]</pre>

<sup>See also: Printing a snake pattern using an array</sup>

huangapple
  • 本文由 发表于 2020年8月10日 14:06:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63334942.html
匿名

发表评论

匿名网友

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

确定