【问题标题】:How to count length of each square (smaller matrix) in matrix如何计算矩阵中每个正方形(较小矩阵)的长度
【发布时间】:2016-02-01 18:29:14
【问题描述】:

我正在解决一项涉及矩阵的算法任务。 我需要在一个较大的正方形中计算每个内部正方形(较小的矩阵)。 原始长度和每个下一个长度之间是否存在依赖关系? 作为一个例子,我有这个矩阵:

1  2  3  4  5  6
7  8  9  10 11 12
13 14 15 16 17 18 
19 20 21 22 23 24 
25 26 27 28 29 30
31 32 33 34 35 36

如你所见Rows = 6Cols = 6Length = Rows * Cols;

问题陈述:如何计算内矩阵的长度:

8  9  10 11
14 15 16 17
20 21 22 23
26 27 28 29

最后一个

15 16
21 22

我能做什么:

for(int i = 0; i < row; i++){ 
    for(int j = 0; j < col; j++){
        //mirrored row and col e.g. 0 1 2 3 2 1 0
        int rowMir = i >= (int)Math.round(M/2.0d) ? (M - 1 - i) : i; 
        int colMir = j >= (int)Math.round(N/2.0d) ? (N - 1 - j) : j; 
        int depth = row > col ? col : row; //depth of inner square for each element in iteration. square's border in other words. 
        //In current matrix there are 3 inner squares (0, 1, 2)
    } 
}

它有用吗?我试图根据我得到的值来计算它,但直到现在还没有成功。在这种情况下,谷歌搜索没有给我任何信息。

编辑 我正在寻找的解决方案是动态获取每个元素的矩阵大小。 [i][j] 的值示例我计算了 depth,它所属的内部矩阵。

【问题讨论】:

  • 原始矩阵是否总是正方形(行 == 列)?
  • 如果外矩阵是6x6,那么根据你对内矩阵的定义,它们的大小将是4x42x2,即每边的大小减少2,对于每个内矩阵。不需要看数据。因此,如果原始矩阵是13x10,那么内部矩阵将是:11x89x67x45x2,并且所有矩阵的长度将是 1308854 , 28, 10.
  • 那么,如果起始矩阵是 4x4,你想要里面所有不同的 1x1、2x2 和 3x3 区域的元素数之和?或者构造一个“内部”矩阵是否意味着只保留不在外边界上的元素?
  • @JeanLogeat,并非总是如此,例如,它可能是 6x5
  • 恐怕我找不到“问题陈述”和显示代码之间的关联。 MN 是什么?当rowcol 永远不会改变时,为什么depth 会一遍又一遍地计算?这两个循环的目的是什么? rowMircolMir 将计算到数字 0-2,因此它们永远不会是问题陈述中显示为第一个内部矩阵的 4x4

标签: java algorithm matrix implementation


【解决方案1】:

您可以在每次迭代中简单地减少2 的步长:

int minDim = Math.min(rows, columns);
for(int i = minDim ; i >= 0 ; i -= 2) {
    System.out.println("Inner length: " + ((rows - i) * (columns - i)))
}

【讨论】:

  • 既然您假设原始矩阵可能不是正方形(我同意),那么您为什么还要假设内部矩阵必须是?每次迭代只需将 rowscolumns 都减少 2,直到其中一个低于 1。
  • @Andreas,这不等同于他正在在做什么吗?
  • 在下面的循环中,我已经证明each element 在循环时需要它。所以它应该类似于(R - depth) * (C - depth)。对于元素[i][j],但不知何故它没有给我正确的答案
  • @Yurets 那么正确答案是什么?从“问题陈述”来看,它似乎是164,即您显示的两个矩阵的长度。这就是你要找的答案吗?如果不是,请澄清问题。
  • 该死,我已经测试了你的解决方案。有用。不知怎的,我错过了它。在我的情况下,我使用已经找到的depth,将它乘以 2 和相同。谢谢你。我接受你的回答:)
【解决方案2】:

如果您真的想打印所有内部矩阵并获取计数,那么您可以执行以下操作:

public static void main (String[] args)
{
    /* Define Matrix */
    int matrixSize = 6;
    int[][] matrix = new int[][]{{1, 2, 3, 4, 5, 6 },
                                 {7, 8, 9, 10,11,12},
                                 {13,14,15,16,17,18},
                                 {19,20,21,22,23,24},
                                 {25,26,27,28,29,30},
                                 {31,32,33,34,35,36}};

    /* Initialize Count Counter */
    int count = 0;

    /* Count/Print Inner Matrices */                             
    for(int x = 2; x < matrixSize; x++) {
        for(int i = 0; i <= matrixSize - x; i++) {
            for(int j = 0; j <= matrixSize - x; j++) {
                /* Call Print Matrix Function */
                printMatrix(matrix, i, j, x);
                ++count; /* Increment Counter */
            }
        }
    }

    /* Print Actual Count */
    System.out.println("\nTotal Inner Square Matrices Count: " + count);
}

/**
 * Print Matrix
 * Arguments: Matrix, Row Index, Column Index, Matrix Size
**/
public static void printMatrix(int[][] matrix, int i, int j, int size) {
    System.out.println();
    /* Row Iterator */
    for(int iIndex = i; iIndex < i + size; iIndex++) {
        System.out.println();
        /* Column Iterator */
        for(int jIndex = j; jIndex < j + size; jIndex++) {
            System.out.print(" " + matrix[iIndex][jIndex]);
        }
    }
}

输出:

1 2
7 8

2 3
8 9

...

Total Inner Matrices: 54

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多