简体   繁体   English

如何循环遍历多维矩阵?

[英]How to cycle through a multi-dimensional matrix?

所以我想循环遍历一个形状为 (82680, 1, 1024, 4) 的多维矩阵,以便能够标记其中哪些值为 0。我如何才能做到这一点?

This might not be the most efficient way to go about this, but an easy way to do this would be这可能不是最有效的方法,但一个简单的方法是

zero_locations = []

for i in range(82680):
    for j in range(1):
        for k in range(1024):
            for l in range(4):
                if matrix[i][j][k][l] == 0:
                    zero_locations.append((i, j, k, l))

What this does is iterate through every item in the matrix, and within the innermost for loop we compare the value of the cell to 0. If the cell is equal to zero, we append a tuple containing the four 'coordinates' to a list.这样做是遍历矩阵中的每一项,在最里面的 for 循环中,我们将单元格的值与 0 进行比较。如果单元格等于 0,我们将包含四个“坐标”的元组附加到列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM