# Matrix implementation in Python

Learn: In this article we are going to **implement matrix operation using list**. Nested list can be used to implement matrix operation. List as an element for another list is called nested list.

Submitted by Abhishek Jain, on October 02, 2017

**Its syntax is:**

a=[[random.random() for col in range(number of column)] for row in range(number of row)]

Here, we need to import random file as **random()** method is used.

### 1) Matrix creation

Program to input matrix with m*n and print the numbers in matrix format.

import random m = input("Enter No. of rows in the matrix: ") n = input("Enter No. of columns in the matrix: ") a = [[random.random() for col in range(n)] for row in range(m)] print "Enter elements: " for i in range(m): for j in range(n): a[i][j] = input() print "output is" for i in range(m): for j in range(n): print a[i][j], '\t', print

Output

Input: Enter No. of rows in the matrix: 3 Enter No. of columns in the matrix: 3 Enter elements: 1 2 3 8 6 3 7 3 5 Output: output is 1 2 3 8 6 3 7 3 5

### 2) Matrix Addition

Suppose A and B are m*n matrices. The sum of A and B, written as A+B, is the m*n matrix obtained by adding corresponding elements from A and B.

import random m1=input("Enter No. of rows in the first matrix: ") n1=input("Enter No. of columns in the first matrix: ") a=[[random.random()for col in range(n1)]for row in range(m1)] print "Enter Elements: " for i in range(m1): for j in range(n1): a[i][j]=input() m2=input("Enter No. of rows in the second matrix: ") n2=input("Enter No. of columns in the second matrix: ") b=[[random.random()for col in range(n2)]for row in range(m2)] print "Enter Elements: " for i in range(m2): for j in range(n2): b[i][j]=input() c=[[random.random()for col in range(n1)]for row in range(m1)] if ((m1==m2) and (n1==n2)): print "output is" for i in range(m1): for j in range(n1): c[i][j] = a[i][j] + b[i][j] print c[i][j], '\t', print else: print "Matrix addition not possible"

Enter No. of rows in the first matrix: 2 Enter No. of columns in the first matrix: 3 Enter Elements: 1 2 3 4 5 6 Enter No. of rows in the second matrix: 2 Enter No. of columns in the second matrix: 3 Enter Elements: 1 2 3 4 5 6 Output: output is 2 4 6 8 10 12

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.

Learn PCB Designing: PCB DESIGNING TUTORIAL