Matrix and Vector in Numpy
- Numpy Import
- Initialize matrix A and B
- Show Matrix A
- Show Matrix B
- Convert Matrix A from List to Numpy Array
- Convert Matrix B from List to Numpy Array
- Initialize constant s
- See how element-wise addition works
- See how element-wise subtraction works
- See how scalar multiplication works
- Divide A by s
- What happens if we have a Matrix + scalar?
- Initialize a 3 by 2 matrix
- Initialize a 2 by 1 matrix
- We expect a resulting matrix of (3 by 2)*(2 by 1) = (3 by 1)
- Initialize vector v
- Show Matrix A again
- Multiply A * v
import numpy as np
A = [[1, 2, 4], [5, 3, 2]]
B = [[1, 3, 4], [1, 1, 1]]
A
B
A_np = np.array(A)
A_np
B_np = np.array(B)
B_np
s = 2
add_AB = A_np + B_np
add_AB
sub_AB = A_np - B_np
sub_AB
mult_As = A_np * s
mult_As
div_As = A_np / s
div_As
add_As = A_np + s
add_As
C_np = np.array([[1, 2], [3, 4],[5, 6]])
C_np
D_np = np.array([1,2])
D_np
mult_CD = C_np @ D_np
mult_CD
v = np.array([[1],[1],[1]])
v
A_np
Av = A_np @ v
Av