如何编写一个用于转置矩阵的方法?

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

How to write a method for a transposed matrix?

问题

我正在尝试创建一个名为MatrixApplication的应用程序类,在其中用户首先输入矩阵的行数和列数。

这将用于创建一个数组对象。
然后按行和列调用Matrix的元素。当所有元素都已读取时,它们将被分配给矩阵对象。

接下来,数组将被转置,最后显示转置后的数组。

如何将元素分配给Matrix对象?
如何显示转置后的数组?

package domain;

public class Matrix {

    private int[][] numbers;

    public Matrix(int rows, int columns) {
        setNumbers(numbers);
        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;
    }
}
package ui;

import java.util.Scanner;
import domain.Matrix;

public class MatrixApplication {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("输入矩阵的行数:");
        int rows = input.nextInt();
        System.out.print("输入矩阵的列数:");
        int columns = input.nextInt();

        Matrix matrix = new Matrix(rows, columns);

        final int[][] numbers = new int[rows][columns];
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < columns; ++j) {
                System.out.printf("输入第 %d 行第 %d 列的元素:", i + 1, j + 1);
                numbers[i][j] = input.nextInt();
            }
        }

        int[][] transposedMatrix = matrix.transpose();
        System.out.println("转置后的矩阵:");
        for (int i = 0; i < transposedMatrix.length; ++i) {
            for (int j = 0; j < transposedMatrix[0].length; ++j) {
                System.out.print(transposedMatrix[i][j] + " ");
            }
            System.out.println();
        }
    }
}

如果您想要将转置后的矩阵以特定形式显示,例如将4x2数组转置为2x4数组,请参考以下示例图像:示例图像。这需要更改转置矩阵的显示方式,以符合所需的格式。

英文:

I am trying to create an application class MatrixApplication, in which the user first enters the number of rows and columns of the matrix.

This will be used to create an array object.
Then the elements of the Matrix are called up row by row and column by column. When all elements have been read in, they are assigned to the matrix object.

Next, the array is transposed and finally the transposed array is displayed.

How do I assign the elements to the Matrix object?
How do I display a transposed array?

package domain;
public class Matrix {
private int[][] numbers;
public Matrix(int rows, int columns) {
setNumbers(numbers);
if (rows &lt; 1)
rows = 1;
else
rows = rows;
if (columns &lt; 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 &lt; numbers.length; ++i) {
for (int j = 0; j &lt; numbers[0].length; ++j) {
transpose[j][i] = numbers[i][j];
}
}
return transpose;
}
}
package ui;
import java.util.Scanner;
import domain.Matrix;
public class MatrixApplication {
public static void main (String[]args)
{
Scanner input = new Scanner (System.in);
System.out.print(&quot;Enter the number of rows of the matrix:&quot;);
int rows = input.nextInt();
System.out.print(&quot;nter the number of columns of the matrix:&quot;);
int colums = input.nextInt();
Matrix matrix = new Matrix(rows, colums);
final int[][] numbers = new int[rows][colums];
for (int i = 0; i &lt; rows; ++i) {
for (int j = 0; j &lt; colums; ++j) {
System.out.printf(&quot;Enter the element of row %d and column %d: &quot;, i + 1, j + 1);
numbers[i][j] = input.nextInt();
}
}
}
System.out.printf(&quot;The transposed matrix: %d&quot;,matrix.transpose());
}
}

And if I want this form of transposed matrix:
example of a 4x2 array to a 2x4 array

答案1

得分: 1

只需将数字读入一个二维数组中,并调用 matrix.setNumbers

final int[][] numbers = new int[rows][columns];
for (int i = 0; i < rows; ++i) {
    for (int j = 0; j < columns; ++j) {
        System.out.printf("Enter the element of row %d and column %d: ", i + 1, j + 1);
        numbers[i][j] = input.nextInt();
    }
}
matrix.setNumbers(numbers);
System.out.printf("The transposed matrix: %s", Arrays.deepToString(matrix.transpose()));
英文:

Simply read the numbers into a two-dimensional array and call matrix.setNumbers.

final int[][] numbers = new int[rows][colums];
for (int i = 0; i &lt; rows; ++i) {
for (int j = 0; j &lt; colums; ++j) {
System.out.printf(&quot;Enter the element of row %d and column %d: &quot;, i + 1, j + 1);
numbers[i][j] = input.nextInt();
}
}
matrix.setNumbers(numbers);
System.out.printf(&quot;The transposed matrix: %s&quot;, Arrays.deepToString(matrix.transpose()));

huangapple
  • 本文由 发表于 2020年8月11日 00:44:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63344435.html
匿名

发表评论

匿名网友

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

确定