
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to find the 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
- Related Articles
- How to find the sum of rows and columns of a given matrix using Numpy?
- How to find the sum of all elements of a given array in JavaScript?
- Find sum of all elements in a matrix except the elements in row and-or column of given cell in Python
- Swift Program to Find the Sum of the Boundary Elements of a Matrix
- How to find the sum of anti-diagonal elements in a matrix in R?
- How to find the sum of all array elements in R?
- How to find all pairs of elements in Java array whose sum is equal to a given number?
- Find the Pair with Given Sum in a Matrix using C++
- Finding the number of rows and columns in a given matrix using Numpy
- C++ code to find out the sum of the special matrix elements
- How to print array elements within a given range using Numpy?
- Minimum operations of given type to make all elements of a matrix equal in C++
- How to find the sum of elements of a Vector using STL in C++?
- Find distinct elements common to all rows of a matrix in Python
- Find distinct elements common to all rows of a Matrix in C++

Advertisements