[英]How can I add all the elements of a Matrix using numpy?
I have a function called check_sum that adds all the elements of an 2d array.我有一个名为 check_sum 的 function ,它添加了二维数组的所有元素。
Grid is a 2d array, and grid_shape is 1024x1024. Grid 是一个二维数组,grid_shape 是 1024x1024。
I'm trying to use the numpy.matrix.sum function, but after several attempts I don't know how to implement it, it always gives me another value.我正在尝试使用 numpy.matrix.sum function,但经过几次尝试我不知道如何实现它,它总是给我另一个价值。
def check_sum(grid):
xmax, ymax = grid_shape
s=0
for i in range(xmax):
for j in range(ymax):
s += grid[i][j]
return s
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.