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

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

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

问题

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

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

  1. public class Matrix {
  2. private int[][] numbers;
  3. public Matrix(int rows, int columns) {
  4. if (rows < 1)
  5. rows = 1;
  6. else
  7. rows = rows;
  8. if (columns < 1)
  9. columns = 1;
  10. else
  11. columns = columns;
  12. numbers = new int[rows][columns];
  13. }
  14. public final void setNumbers(int[][] numbers) {
  15. this.numbers = numbers;
  16. }
  17. public int[][] getNumbers() {
  18. return numbers;
  19. }
  20. public int[][] transpose() {
  21. int[][] transpose = new int[numbers[0].length][numbers.length];
  22. for (int i = 0; i < numbers.length; i++) {
  23. for (int j = 0; j < numbers[0].length; j++) {
  24. transpose[j][i] = numbers[i][j];
  25. }
  26. }
  27. return transpose;
  28. }
  29. }
英文:

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?

  1. public class Matrix {
  2. private int[][] numbers;
  3. public Matrix(int rows, int colums) {
  4. if (rows &lt; 1)
  5. rows = 1;
  6. else
  7. rows = rows;
  8. if (colums &lt; 1)
  9. colums = 1;
  10. else
  11. colums = colums;
  12. numbers = new int[rows][colums];
  13. }
  14. public final void setNumbers(int[][] numbers) {
  15. this.numbers = numbers;
  16. }
  17. public int[][] getNumbers() {
  18. return numbers;
  19. }
  20. public int[][] transpose() {
  21. int[][] transpose = getNumbers();
  22. return numbers;
  23. }
  24. }

答案1

得分: 8

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

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

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

  1. /**
  2. * Transposses a matrix.
  3. * Assumption: mat is a non-empty matrix. i.e.:
  4. * 1. mat != null
  5. * 2. mat.length &gt; 0
  6. * 3. For every i, mat[i].length are equal and mat[i].length &gt; 0
  7. */
  8. public static int[][] transpose(int[][] mat) {
  9. int[][] result = new int[mat[0].length][mat.length];
  10. for (int i = 0; i &lt; mat.length; ++i) {
  11. for (int j = 0; j &lt; mat[0].length; ++j) {
  12. result[j][i] = mat[i][j];
  13. }
  14. }
  15. return result;
  16. }

答案2

得分: 1

矩阵的转置

  1. int m = 4;
  2. int n = 5;
  1. // 原始矩阵
  2. int[][] arr1 = {
  3. {11, 12, 13, 14, 15},
  4. {16, 17, 18, 19, 20},
  5. {21, 22, 23, 24, 25},
  6. {26, 27, 28, 29, 30}};
  7. // 转置矩阵
  8. int[][] arr2 = new int[n][m];
  1. // 交换行和列
  2. IntStream.range(0, n).forEach(i ->
  3. IntStream.range(0, m).forEach(j ->
  4. arr2[i][j] = arr1[j][i]));

  1. // 输出到 Markdown 表格
  2. String matrices = Stream.of(arr1, arr2)
  3. .map(arr -> Arrays.stream(arr).map(Arrays::toString)
  4. .collect(Collectors.joining("<br>")))
  5. .collect(Collectors.joining("</pre> | <pre>"));
  6. System.out.println("| 原始矩阵 | 转置矩阵 |");
  7. System.out.println("|---|---|");
  8. System.out.println("| <pre>" + matrices + "</pre> |");
原始矩阵 转置矩阵
  1. [11, 12, 13, 14, 15]
    [16, 17, 18, 19, 20]
    [21, 22, 23, 24, 25]
    [26, 27, 28, 29, 30]
  1. [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

  1. int m = 4;
  2. int n = 5;
  1. // original matrix
  2. int[][] arr1 = {
  3. {11, 12, 13, 14, 15},
  4. {16, 17, 18, 19, 20},
  5. {21, 22, 23, 24, 25},
  6. {26, 27, 28, 29, 30}};
  7. // transposed matrix
  8. int[][] arr2 = new int[n][m];
  1. // swap rows and columns
  2. IntStream.range(0, n).forEach(i -&gt;
  3. IntStream.range(0, m).forEach(j -&gt;
  4. arr2[i][j] = arr1[j][i]));

  1. // output to the markdown table
  2. String matrices = Stream.of(arr1, arr2)
  3. .map(arr -&gt; Arrays.stream(arr).map(Arrays::toString)
  4. .collect(Collectors.joining(&quot;&lt;br&gt;&quot;)))
  5. .collect(Collectors.joining(&quot;&lt;/pre&gt; | &lt;pre&gt;&quot;));
  6. System.out.println(&quot;| original matrix | transposed matrix |&quot;);
  7. System.out.println(&quot;|---|---|&quot;);
  8. 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:

确定