TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python Add Two Matrices

Python Add Two Matrices for beginners and professionals with programs on basics, controls, loops, functions, native data types etc.

<< Back to PYTHON

Python Program to Add Two Matrices

What is Matrix?

In mathematics, matrix is a rectangular array of numbers, symbols or expressions arranged in the form of rows and columns. For example: if you take a matrix A which is a 2x3 matrix then it can be shown like this:

2       3          5
8       12        7

Image representation:

Python Nativ Data Programs1

In Python, matrices can be implemented as nested list. Each element of the matrix is treated as a row. For example X = [[1, 2], [3, 4], [5, 6]] would represent a 3x2 matrix. First row can be selected as X[0] and the element in first row, first column can be selected as X[0][0].

Let's take two matrices X and Y, having the following value:

X = [[1,2,3],
    [4,5,6],
    [7,8,9]]

Y = [[10,11,12],
    [13,14,15],
    [16,17,18]]

Create a new matrix result by adding them.

See this example:

X = [[1,2,3],
       [4,5,6],
       [7,8,9]]

Y = [[10,11,12],
       [13,14,15],
       [16,17,18]]

Result = [[0,0,0],
                [0,0,0],
                [0,0,0]]
# iterate through rows
for i in range(len(X)):
   # iterate through columns
   for j in range(len(X[0])):
       result[i][j] = X[i][j] + Y[i][j]
for r in result:
   print(r)

Output:

Python Nativ Data Programs2



Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf