{"id":20190,"date":"2021-09-06T13:41:54","date_gmt":"2021-09-06T08:11:54","guid":{"rendered":"https:\/\/python-programs.com\/?p=20190"},"modified":"2021-11-22T18:36:26","modified_gmt":"2021-11-22T13:06:26","slug":"python-program-to-find-the-sum-of-all-elements-in-a-2d-array-matrix","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-sum-of-all-elements-in-a-2d-array-matrix\/","title":{"rendered":"Python Program to Find the Sum of all Elements in a 2D Array\/Matrix"},"content":{"rendered":"

In the previous article, we have discussed Python Program to Display an Upper Triangular Matrix<\/a><\/p>\n

Given a matrix and the task is to find the sum of all elements in a 2-dimensional array of the given matrix in Python.<\/p>\n

What is a matrix:<\/strong><\/p>\n

A matrix is a rectangular sequence of numbers divided into columns and rows. A matrix element or entry is a number that appears in a matrix.<\/p>\n

Example:<\/strong><\/p>\n

\"\"<\/p>\n

Above is the matrix which contains 5 rows and 4 columns and having elements from 1 to 20.<\/p>\n

In this order, the dimensions of a matrix indicate the number of rows and columns.<\/p>\n

Here as there are 5 rows and 4 columns it is called a 5*4 matrix.<\/p>\n

Examples:<\/strong><\/p>\n

Example1:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given Matix : \r\n1 6 4 \r\n8 1 3 \r\n1 6 2<\/pre>\n

Output:<\/strong><\/p>\n

The sum of all elements in 2 dimensional array for a given matrix is :\r\n32<\/pre>\n

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given Matix : \r\n0 8\r\n3 7<\/pre>\n

Output:<\/strong><\/p>\n

The sum of all elements in 2 dimensional array for a given matrix is :\r\n18<\/pre>\n

Program to Find the Sum of all Elements in a 2D Array in Python<\/h2>\n

Below are the ways to find the sum of all elements in a 2-dimensional array of the given matrix in Python:<\/p>\n