如何使用Numpy查找给定矩阵的所有元素的总和?
在这个程序中,我们将使用sum()numpy库中的函数添加一个numpy矩阵的所有项。我们将首先创建一个随机的numpy矩阵,然后我们将获得所有元素的总和。
算法
Step 1: Import numpy. Step 2: Create a random m×n matrix using the random() function. Step 3: Obtain the sum of all the elements in the matrix using the sum() function.
示例代码
import numpy as np
matrix = np.random.rand(3,3)
print("The numpy matrix is: \n",matrix)
print("\nThe sum of the matrix is: ", np.sum(matrix))输出结果The numpy matrix is: [[0.66411969 0.43672579 0.48448593] [0.76110384 0.35869136 0.51509622] [0.22408857 0.17984855 0.33566272]] The sum of the matrix is: 3.9598226605128524
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短