Multiplication of arrays

Multiplication of arrays

This is a brief note but something that you may find yourself doing a lot when working wih numpy. The great thing about arrays is that you can quickly multiply and manipulate them without having to specifically loop through each element.

Create 2 2d arrays of random number between zero and 10:

import numpy

To multiply the elements of random_1 by random_2, you can type (and assign it to its own variable) using:


Now, both random_1 and random_2 are matrices. Using the above operation is a pair wise multiplication (e.g. element 1 of random_1 multiplied by element 1 of random_2). To actually carry out a true (mathematical) matrix multiplication you must type:

Anyway, just so you know and are aware of the difference.

There is a lot of useful information on nummerical operations for arrays available here.

Previous Home Next