给定一个 numpy 矩阵,my_matrix
。
import numpy as np
my_matrix = np.array([[1.2,2.3,None],[4.5,3.4,9.3]])
如何有效地将其展平为以下包含 my_matrix
索引位置的数组?
[[0,0],[0,1],[0,2],[1,0],[1,1],[1,2]]
最佳答案
你可以尝试:
rows, cols = my_matrix.shape
[[i, j] for i in range(rows) for j in range(cols)]
输出:
[[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2]]
关于python - 将矩阵展平为包含值索引位置的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57598824/