使用 Python 检查二进制矩阵中的水平和垂直对称性


二进制矩阵是一个矩形网格,其中每个元素要么是 0 要么是 1,表示真或假状态。它广泛用于表示各个学科中的关系、连接性和模式。

假设我们已经获取了一个包含 N 行 M 列的二维二进制输入矩阵。我们将使用以下方法检查输入矩阵是否水平或垂直对称或两者兼而有之。

如果第一行与最后一行匹配,第二行与倒数第二行匹配,依此类推,则称该矩阵为水平对称。

如果第一列与最后一列匹配,第二列与倒数第二列匹配,依此类推,则称该矩阵为垂直对称。

示例

1 0 1
0 1 0
1 0 1

水平对称:当矩阵水平翻转时,第一行“1 0 1”是第三行“1 0 1”的精确镜像反射。第一行中的每个元素都对应于第三行中相同位置的元素。

垂直对称:当矩阵垂直翻转时,第一列“1 0 1”是第三列“1 0 1”的镜像版本。第一列中的元素与第三列中相同位置的元素对齐。

算法(步骤)

以下是执行所需任务的算法/步骤。

  • 创建一个函数checkHorizontalVertical(),通过将输入矩阵、行数和列数作为参数来检查输入矩阵是否水平或垂直对称。

  • 将horizontal_symmetric和vertical_symmetric初始化为True,以假设矩阵在两个方向上都是对称的。

  • 比较第一行与最后一行、第二行与倒数第二行,依此类推,以检查水平对称性。

  • 遍历矩阵一半的行。

  • 在循环内,遍历列并比较当前行的每个单元格与正在比较的行中相应单元格。

  • 如果任何单元格不同,则将horizontal_symmetric设置为False并中断循环。

  • 为下一次迭代,将行号加1,并将最后一行中的行号减1。

  • 接下来,通过比较第一列与最后一列、第二列与倒数第二列,依此类推,检查垂直对称性。

  • 遍历矩阵一半的列。

  • 在循环内,遍历行并比较当前列的每个单元格与正在比较的列中相应单元格。

  • 如果任何单元格不同,则将vertical_symmetric设置为False并中断循环。

  • 为下一次迭代,将列号加1,并将最后一列中的列号减1。

  • 检查对称条件

    • 如果horizontal_symmetric和vertical_symmetric都不为True,则打印“既不是水平对称也不是垂直对称”。

    • 如果horizontal_symmetric为True但vertical_symmetric为False,则打印“输入矩阵水平对称”。

    • 如果vertical_symmetric为True但horizontal_symmetric为False,则打印“输入矩阵垂直对称”。

    • 如果horizontal_symmetric和vertical_symmetric都为True,则打印“输入矩阵水平和垂直都对称”。

  • 使用inputMatrix、行数和列数作为参数调用checkHorizontalVertical函数。

示例

以下示例比较了行与其从末尾开始的对应行,以检查水平对称性,并比较了列与其从末尾开始的对应列,以检查垂直对称性。根据比较结果,它打印输入矩阵是水平对称、垂直对称、水平和垂直都对称,还是既不是水平对称也不是垂直对称。

# Creating a function to check whether the input matrix is
# horizontal or vertically symmetric by passing the input matrix,
# no of rows and columns as arguments
def checkHorizontalVertical(inputMatrix, rows, cols):

    # Initializing both the horizontal and vertical
    # symmetric as true at first
    horizontal_symmetric = True
    vertical_symmetric = True

    # We compare the first row with the last row, the second row with second
    # last row and so on.
    p = 0
    x = rows - 1
    # Traversing till half of the rows of the matrix
    while p < rows // 2:

        # Traversing in the columns
        for q in range(cols):
            # Checking if each cell is the same or not
            if inputMatrix[p][q] != inputMatrix[x][q]:
                # If it is not the same then that horizontal symmetric is false
                horizontal_symmetric = False
                break
                # Incrementing the row number by 1
        p += 1
        x -= 1

    # Checking for Vertical Symmetry. The first column is compared to the last column,
    # the second column to the second-to-last column, and so forth.
    p = 0
    x = cols - 1
    # Traversing till half of the columns
    while p < cols // 2:

        # Checking each row of the column
        for q in range(rows):

            # Checking if each cell is the same or not
            if inputMatrix[p][q] != inputMatrix[x][q]:
                # If it is not the same then that vertical symmetric is false
                vertical = False
                break
                # Incrementing the column number by 1
        p += 1
        x -= 1

        # checking whether not horizontal and not vertically symmetric
    if not horizontal_symmetric and not vertical_symmetric:
        # printing Neither horizontal nor vertical symmetric if the condition is true
        print("Neither horizontal nor vertical symmetric")
        # checking whether the matrix is horizontally symmetric but not vertical
    elif horizontal_symmetric and not vertical_symmetric:
        # printing horizontal symmetric matrix
        print("The input matrix is horizontal symmetric")
        # checking whether the matrix is vertically symmetric but not horizontal
    elif vertical_symmetric and not horizontal_symmetric:
        # printing vertically symmetric
        print("The input matrix is vertical symmetric")
    else:
        # else printing both horizontal and vertical symmetric
        print("The input matrix is both horizontal and vertically symmetric")


# input matrix
inputMatrix = [[1, 0, 1], [0, 0, 0], [1, 0, 1]]


# calling the above checkHorizontalVertical() by passing
# input matrix, no of rows, columns as arguments
checkHorizontalVertical(inputMatrix, 3, 3)

输出

The input matrix is both horizontal and vertically symmetric

结论

我们在本文中介绍了如何使用嵌套for循环遍历矩阵,以及如何使用for循环遍历每列的行。最后,我们学习了如何确定特定矩阵是水平对称还是垂直对称。

更新于: 2023年8月17日

266 次浏览

开启您的 职业生涯

通过完成课程获得认证

立即开始
广告

© . All rights reserved.