
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Sum of All Elements of a Given Matrix Using NumPy
In this program, we will add all the terms of a numpy matrix using the sum() function in the numpy library. We will first create a random numpy matrix and then, we will obtain the sum of all the elements.
Algorithm
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.
Example Code
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))
Output
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
Advertisements