Title: Adding And Subtracting Matrices
Slug: adding_and_subtracting_matrices
Summary: How to add and subtract matrices in Python.
Date: 2017-09-03 12:00
Category: Machine Learning
Tags: Vectors Matrices Arrays
Authors: Chris Albon

Preliminaries


In [9]:
# Load library
import numpy as np

Create Matrices


In [10]:
# Create matrix
matrix_a = np.array([[1, 1, 1],
                     [1, 1, 1],
                     [1, 1, 2]])

# Create matrix
matrix_b = np.array([[1, 3, 1],
                     [1, 3, 1],
                     [1, 3, 8]])

Add Matrices


In [11]:
# Add two matrices
np.add(matrix_a, matrix_b)


Out[11]:
array([[ 2,  4,  2],
       [ 2,  4,  2],
       [ 2,  4, 10]])

Subtract Matrices


In [12]:
# Subtract two matrices
np.subtract(matrix_a, matrix_b)


Out[12]:
array([[ 0, -2,  0],
       [ 0, -2,  0],
       [ 0, -2, -6]])