How to convert multiple matrices into one single vector in Python?

This is a quick post on how to convert multiple matrices into a single vector using Python’s numpy package. To begin with let us define 2 matrices.

What we want to do is to merge the contents of mat1 and mat2 into a single vector. To reach that goal first we need to convert each of them into vectors. For this we will make use of Numpy’s reshape.

The -1 value for the newshape parameter of reshape ensures that the output has only 1 dimension. A matrix with 1 dimension is called a vector, which is what we want to achieve. Now that we have flattened both the matrices, we can merge (concatenate) them into a single vector using

So the final script and it’s output look like this

 

 

By | 2017-04-24T20:38:51+00:00 April 24th, 2017|languages, python|0 Comments

Leave A Comment