How to do it…

Let's look the the different methods.

Matrix addition

In order to understand how matrix addition is done, we will first initialize two arrays:

# Initializing an arrayx = np.array([[1, 1], [2, 2]])y = np.array([[10, 10], [20, 20]])

Similar to what we saw in a previous chapter, we initialize a 2 x 2 array by using the np.array function.

There are two methods by which we can add two arrays.

Method 1

A simple addition of the two arrays x and y can be performed as follows:

x+y

Note that x evaluates to:

[[1 1] [2 2]]

y evaluates to:

[[10 10] [20 20]]

The result of x+y would be equal to:

[[1+10 1+10] [2+20 2+20]]

Finally, this gets evaluated to:

[[11 11] [22 22]]

Method 2

The same preceding operation can also be performed by using ...

Get SciPy Recipes now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.