如何拆分矩阵,然后使用JavaScript显示其最小值和最大值?

发布于 2025-02-08 20:14:33 字数 361 浏览 0 评论 0原文

我有一个矩阵,

const matrix = [
      [1,5,7],
      [7,3,5],
      [2,6,9]
];

我想将其分成子量表,

[
 [1,5],
 [7,3]
]
[
 [5,7],
 [3,5]
]
[
 [7,3],
 [2,6]
]
[
 [3,5],
 [6,9]
]

中获取最大值

[
 [7,7],
 [7,9]
]

然后我想从子词和最小值

[
  [1,3],
  [2,3]
]

。我该如何处理?

I have a matrix

const matrix = [
      [1,5,7],
      [7,3,5],
      [2,6,9]
];

I would like to split it into sub-matrices

[
 [1,5],
 [7,3]
]
[
 [5,7],
 [3,5]
]
[
 [7,3],
 [2,6]
]
[
 [3,5],
 [6,9]
]

Then I want to get the maximum values from the sub-matrices

[
 [7,7],
 [7,9]
]

and also the minimum values.

[
  [1,3],
  [2,3]
]

How can I approach this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

欢你一世 2025-02-15 20:14:33

由于未指定原始矩阵的尺寸和子膜的尺寸,因此我以示例中给出的尺寸:

// main function
public static void functionMatrix() {
    int[][] matrix = {{1, 5, 7}, {7, 3, 5}, {2, 6, 9}};

    int[][] matrixResultMax = new int[matrix.length - 1][matrix[0].length - 1];
    int[][] matrixResultMin = new int[matrix.length - 1][matrix[0].length - 1];

    for (int i = 0; i < matrix.length - 1; i++) {
        for (int j = 0; j < matrix[i].length - 1; j++) {
            //Submatrix elements
            System.out.println("[");
            System.out.println("[ " + matrix[i][j] + ", " + matrix[i][j + 1] + " ],");
            System.out.println("[ " + matrix[i + 1][j] + ", " + matrix[i + 1][j + 1] + " ]");
            matrixResultMax[i][j] = Math.max(Math.max(matrix[i][j], matrix[i][j + 1]), Math.max(matrix[i + 1][j], matrix[i + 1][j + 1]));
            matrixResultMin[i][j] = Math.min(Math.min(matrix[i][j], matrix[i][j + 1]), Math.min(matrix[i + 1][j], matrix[i + 1][j + 1]));
            System.out.println("]");
        }
    }

    printMatrix(matrixResultMax);
    printMatrix(matrixResultMin);
}

//new Function
public static void printMatrix(int[][] matr) {
    for (int[] ints : matr) {
        for (int anInt : ints) {
            System.out.print(anInt + " ");
        }
        System.out.println();
    }
}

Since the dimensions of the original matrix and the dimensions of the submatrices are not specified, I took the ones given in the example:

// main function
public static void functionMatrix() {
    int[][] matrix = {{1, 5, 7}, {7, 3, 5}, {2, 6, 9}};

    int[][] matrixResultMax = new int[matrix.length - 1][matrix[0].length - 1];
    int[][] matrixResultMin = new int[matrix.length - 1][matrix[0].length - 1];

    for (int i = 0; i < matrix.length - 1; i++) {
        for (int j = 0; j < matrix[i].length - 1; j++) {
            //Submatrix elements
            System.out.println("[");
            System.out.println("[ " + matrix[i][j] + ", " + matrix[i][j + 1] + " ],");
            System.out.println("[ " + matrix[i + 1][j] + ", " + matrix[i + 1][j + 1] + " ]");
            matrixResultMax[i][j] = Math.max(Math.max(matrix[i][j], matrix[i][j + 1]), Math.max(matrix[i + 1][j], matrix[i + 1][j + 1]));
            matrixResultMin[i][j] = Math.min(Math.min(matrix[i][j], matrix[i][j + 1]), Math.min(matrix[i + 1][j], matrix[i + 1][j + 1]));
            System.out.println("]");
        }
    }

    printMatrix(matrixResultMax);
    printMatrix(matrixResultMin);
}

//new Function
public static void printMatrix(int[][] matr) {
    for (int[] ints : matr) {
        for (int anInt : ints) {
            System.out.print(anInt + " ");
        }
        System.out.println();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文