A matrix can only be added to or subtracted from another matrix if the two matrices have the same dimensions.
Suppose we have two matrices E and D.
Example 1:
E = [[1,2],[3,4]]
DY = [[4,5],[6,7]]
then we get
E+D = [[5,7],[9,11]]
Example 2:
E = [[1,2],[3,4],[8,3]]
D = [[4,5],[6,7],[4,7]]
then we get
E+D = [[5,7],[9,11],[12,10]]
Look at the example given below to understand how to add the elements in Python:
Source Code:
import numpy as np
  # The first matrix will be
P = np.array([[1, 2], [3, 4]])
#The second matrix will be
Q = np.array([[4, 5], [6, 7]])
print("Elements of the first matrix")
print(P)
print("Elements of the second matrix")
print(Q)
  # adding two matrix
print("The sum of the two matrices is")
print(np.add(P, Q))
Output:
Elements of the first matrix
[[1 2]
 [3 4]]
Elements of the second matrix
[[4 5]
 [6 7]]
The sum of the two matrices is
[[ 5 7]
 [ 9 11]]
Related Questions
- How do you make a vowel counter in Python?
- How do I remove punctuation from a list in Python?
- How do you transpose a matrix in python?
- How do you find the sum of a matrix in python?
- How do you count the number of vowels in a list in Python?
- How do I remove punctuation from a text file in Python?
- How do you transpose in Python?
- How do you extract a vowel from a string in Python?
- How do you remove special and punctuation characters in Python?
- How do you add two matrices in Python using Numpy?
- Is vowel function in Python?
- How do I remove special characters from a list in Python?
- How do you transpose amatrix using Numpy in Python?
Leave a Reply