# Adding and Subtracting Matrices in Python

In this article, we will discuss how to add and subtract elements of the matrix in Python.

Example:

```Suppose we have two matrices A and B.
A = [[1,2],[3,4]]
B = [[4,5],[6,7]]

then we get
A+B = [[5,7],[9,11]]
A-B = [[-3,-3],[-3,-3]]```

Now let us try to implement this using Python

1. Adding elements of the matrix

In the above code, we have used np.add() method to add elements of two matrices. If shape of two arrays are not same, that is arr1.shape != arr2.shape, they must be broadcastable to a common shape (which may be the shape of one or the other).

## Python3

 `# importing numpy as np` `import` `numpy as np`     `# creating first matrix` `A ``=` `np.array([[``1``, ``2``], [``3``, ``4``]])`   `# creating second matrix` `B ``=` `np.array([[``4``, ``5``], [``6``, ``7``]])`   `print``(``"Printing elements of first matrix"``)` `print``(A)` `print``(``"Printing elements of second matrix"``)` `print``(B)`   `# adding two matrix` `print``(``"Addition of two matrix"``)` `print``(np.add(A, B))`

Output:

```Printing elements of first matrix
[[1 2]
[3 4]]
Printing elements of second matrix
[[4 5]
[6 7]]
[[ 5  7]
[ 9 11]]```

2. Subtracting elements of matrices

In the above code, we have used np.subtract() to subtract elements of two matrices. It returns the difference of arr1 and arr2, element-wise.

## Python3

 `# importing numpy as np` `import` `numpy as np`     `# creating first matrix` `A ``=` `np.array([[``1``, ``2``], [``3``, ``4``]])`   `# creating second matrix` `B ``=` `np.array([[``4``, ``5``], [``6``, ``7``]])`   `print``(``"Printing elements of first matrix"``)` `print``(A)` `print``(``"Printing elements of second matrix"``)` `print``(B)`   `# subtracting two matrix` `print``(``"Subtraction of two matrix"``)` `print``(np.subtract(A, B))`

Output:

```Printing elements of first matrix
[[1 2]
[3 4]]
Printing elements of second matrix
[[4 5]
[6 7]]
Subtraction of two matrix
[[-3 -3]
[-3 -3]]```

METHOD 3:Using nested loops

APPROACH:

The given task is to subtract two matrices in Python and print their elements. Approach 2 uses nested loops to subtract the two matrices. It prints the elements of both matrices using nested loops and then subtracts the corresponding elements of the matrices to get the result matrix.

ALGORITHM:

1. Define two matrices matrix1 and matrix2.
2.Print the elements of matrix1 and matrix2 using nested loops.
3. Define an empty matrix result of the same size as matrix1.
4. Subtract the corresponding elements of matrix1 and matrix2 using nested loops and store the result in the result matrix.
5. Print the result matrix.

## Python3

 `# Input matrices` `matrix1 ``=` `[[``1``, ``2``], [``3``, ``4``]]` `matrix2 ``=` `[[``4``, ``5``], [``6``, ``7``]]`   `# Printing elements of matrix1` `print``(``"Printing elements of first matrix"``)` `for` `row ``in` `matrix1:` `    ``for` `element ``in` `row:` `        ``print``(element, end``=``" "``)` `    ``print``()`   `# Printing elements of matrix2` `print``(``"Printing elements of second matrix"``)` `for` `row ``in` `matrix2:` `    ``for` `element ``in` `row:` `        ``print``(element, end``=``" "``)` `    ``print``()`   `# Subtracting two matrices` `result ``=` `[[``0``, ``0``], [``0``, ``0``]]` `for` `i ``in` `range``(``len``(matrix1)):` `    ``for` `j ``in` `range``(``len``(matrix1[``0``])):` `        ``result[i][j] ``=` `matrix1[i][j] ``-` `matrix2[i][j]`   `# Printing the result` `print``(``"Subtraction of two matrix"``)` `for` `row ``in` `result:` `    ``for` `element ``in` `row:` `        ``print``(element, end``=``" "``)` `    ``print``()`

Output

```Printing elements of first matrix
1 2
3 4
Printing elements of second matrix
4 5
6 7
Subtraction of two matrix
-3 -3
-3 -3 ```

Time complexity:

1. The time complexity of printing the matrices using nested loops is O(n^2), where n is the size of the matrices.
2. The time complexity of subtracting two matrices using nested loops is also O(n^2).
3. Therefore, the overall time complexity of this approach is O(n^2).

Space complexity:

1.The space complexity of this approach is O(n^2), as it requires three matrices of size n^2. The input matrices matrix1 and matrix2 require a space of n^2 each, and the result matrix requires a space of n^2.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Previous
Next