Python and NumPy: Concatenation of Row Vectors, Column Vectors and Matrices

Tutorial Video

Perquisites

Python

Note for MATLAB/Octave Users

If you are transitioning from Python from MATLAB or Octave the first three things that you will have to address is the notion of zero-order indexing, the means to specify a row or column vector and the subtle differences between what bracket type to use.

For indexing MATLAB starts indexing from 1 so goes 1,2,3,… whereas Python indexes from 0 so goes 0,1,2,..

In MATLAB two delimiters are used comma , and semi-colon ;. The , moves to the next column and the ; moves to a new row.

\displaystyle \left[ {\begin{array}{*{20}{c}} 1 & 2 \\ 3 & 4 \end{array}} \right]

To create this in MATLAB one would use:

HTML

In Python there is only the comma , delimiter and we need to learn how to create matrices with only one delimiter. This is done through the use of multiple square [ ] brackets to create a list of lists:

Python

For array creation MATLAB and Python both use [ ] brackets.

For indexing MATLAB uses ( ) brackets while Python uses [ ] brackets.

For inputs to functions MATLAB and Python both use ( ) brackets.

For multiple outputs to functions MATLAB and Python both use [ ] brackets.

Zero-Order Indexing of a Vector

Let us create a vector using the function arange:

Python
[0 1 2 3 4 5 6 7 8 9]

Recall that when arange is used, we count from 0 and go up in integer steps to the value of 10 but don’t reach the value of 10. This is known as 0 order indexing and if counting with our fingers we start with 0 and get to 9.

Thus for the following:

Python
[9, 4, 6, 8, 3, 7, 2, 1, 6, 12]
[0 1 2 3 4 5 6 7 8 9]

We can see the 0th element of v2 is 9:

\displaystyle \text{v2}=\left[ {\begin{array}{*{20}{c}} {\underset{0}{\overset{{-10}}{\mathop 9}}\,} & {\underset{1}{\overset{{-9}}{\mathop 4}}\,} & {\underset{2}{\overset{{-8}}{\mathop 6}}\,} & {\underset{3}{\overset{{-7}}{\mathop 8}}\,} & {\underset{4}{\overset{{-6}}{\mathop 3}}\,} & {\underset{5}{\overset{{-5}}{\mathop 7}}\,} & {\underset{6}{\overset{{-4}}{\mathop 2}}\,} & {\underset{7}{\overset{{-3}}{\mathop 1}}\,} & {\underset{8}{\overset{{-2}}{\mathop 6}}\,} & {\underset{9}{\overset{{-1}}{\mathop {12}}}\,} \end{array}} \right]

We can index a single value using square brackets:

Python
9
Python
12

We can also index negative values:

Python
12

We can also index multiple values using a comma for instance:

Python
[4 6]

Or if all the elements we wish to select are beside each other we can index using a colon:

Python

Recall once again we are using 0 order indexing and start at the specified value and go up to in integer steps, up to but not including the final value (similar to the function arange).

Python

Using a colon alone we can select every single value:

Python
[ 9  4  6  8  3  7  2  1  6 12]

We can use a double colon and then a 2 to select every second value or 3 for every third value:

Python
[ 9  4  6  8  3  7  2  1  6 12]

[9 6 3 2 6]
Python
[ 9  4  6  8  3  7  2  1  6 12]

[ 9  8  2 12]

Zero-Order Indexing of a Matrix

For a more complicated object such as a matrix we need two values, Axis 0 for the Row and Axis 1 for the Column. Let’s create a 2 by 3 matrix i.e. 2 rows and 3 columns.

Python
[[1 2 3]
 [4 5 6]]

\displaystyle \text{M}=\left[ {\begin{array}{*{20}{c}} {\underset{{\left[ {0,0} \right]}}{\overset{{\left[ {-2,-3} \right]}}{\mathop 1}}\,} & {\underset{{\left[ {0,1} \right]}}{\overset{{\left[ {-2,-2} \right]}}{\mathop 2}}\,} & {\underset{{\left[ {0,2} \right]}}{\overset{{\left[ {-2,-1} \right]}}{\mathop 3}}\,} \\ {\underset{{\left[ {1,0} \right]}}{\overset{{\left[ {-1,-3} \right]}}{\mathop 4}}\,} & {\underset{{\left[ {1,1} \right]}}{\overset{{\left[ {-1,-2} \right]}}{\mathop 5}}\,} & {\underset{{\left[ {1,2} \right]}}{\overset{{\left[ {-1,-1} \right]}}{\mathop 6}}\,} \end{array}} \right]

Python
6
Python
5

Indexing Multiple Elements

To index multiple elements, a vector can be input for each value. For instance if we wished to make the selection:

\displaystyle \left[ {\begin{array}{*{20}{c}} \mathbf{1} & 2 & \mathbf{3} \\ 4 & \mathbf{5} & 6 \end{array}} \right]

Python
[1 3 5]

Selecting an Entire Column or Row

To select a full column or row we can once again index using a colon.

For instance if we want to select the 1st row from the matrix below. This can be thought of as all elements of the first row. This means all columns which can be selected using a colon for the colunn index and the first row, so 1 as the row index:

\displaystyle \left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 \\ \mathbf{4} & \mathbf{5} & \mathbf{6} \end{array}} \right]

Python
[4 5 6]

If we want all rows of the 0th column instead:

\displaystyle \left[ {\begin{array}{*{20}{c}} \mathbf{1} & 2 & 3 \\ \mathbf{4} & 5 & 6 \end{array}} \right]

We can specify : for the row index and 0 for the column index:

Python
[1,4]

Note how the result yields a row vector opposed to a column vector. We will discuss this in detail in a moment.

Selecting Every nth Value of a Matrix

Similar to a vector, we can use the double colon followed by a number x to select every x value. To select the following:

\displaystyle \left[ {\begin{array}{*{20}{r}} \mathbf{1} & 2 & \mathbf{3} \\ \mathbf{4} & 5 & \mathbf{6} \end{array}} \right]

This is every row, so this can be selected using a colon :, and its every second column so we can select this using a double colon followed by a 2 i.e. ::2

Python
[[1 3]
 [4 6]]

Transpose of a Matrix

Transposing a matrix flips the rows and columns in a matrix. For a matrix with m rows and n columns. Flipping m↔n gives:

\displaystyle \left[ {\begin{array}{*{20}{c}} {\underset{{0,0}}{\overset{{m,n}}{\mathop{1}}}\,} & {\underset{{0,1}}{\overset{{m,n}}{\mathop{2}}}\,} & {\underset{{0,2}}{\overset{{m,n}}{\mathop{3}}}\,} \\ {\underset{{1,0}}{\overset{{m,n}}{\mathop{4}}}\,} & {\underset{{1,1}}{\overset{{m,n}}{\mathop{5}}}\,} & {\underset{{1,2}}{\overset{{m,n}}{\mathop{6}}}\,} \end{array}} \right]

\displaystyle \left[ {\begin{array}{*{20}{c}} {\underset{{0,0}}{\overset{{n,m}}{\mathop{1}}}\,} & {\underset{{1,0}}{\overset{{n,m}}{\mathop{4}}}\,} \\ {\underset{{0,1}}{\overset{{n,m}}{\mathop{2}}}\,} & {\underset{{1,1}}{\overset{{n,m}}{\mathop{5}}}\,} \\ {\underset{{0,2}}{\overset{{n,m}}{\mathop{3}}}\,} & {\underset{{1,2}}{\overset{{n,m}}{\mathop{6}}}\,} \end{array}} \right]

Which is the following when m denotes the row index and n denotes the column index:

\displaystyle \left[ {\begin{array}{*{20}{c}} {\underset{{0,0}}{\overset{{m,n}}{\mathop{1}}}\,} & {\underset{{1,0}}{\overset{{m,n}}{\mathop{4}}}\,} \\ {\underset{{0,1}}{\overset{{m,n}}{\mathop{2}}}\,} & {\underset{{1,1}}{\overset{{m,n}}{\mathop{5}}}\,} \\ {\underset{{0,2}}{\overset{{m,n}}{\mathop{3}}}\,} & {\underset{{1,2}}{\overset{{m,n}}{\mathop{6}}}\,} \end{array}} \right]

Python
[[1 2 3]
 [4 5 6]]

[[1 4]
 [2 5]
 [3 6]]

Concatenation of Matrices

Python
[[1 2 3]
 [4 5 6]
 [1 2 3]
 [4 5 6]]
Python
[[1 2 3 1 2 3]
 [4 5 6 4 5 6]]

Transpose of a Vector

Recall that when we used a colon to select a Column of a Matrix that the result returned the Column as a Row vector. We can have a look at this in some more detail now. In Python the transpose of a vector returns the vector unchanged:

Python
[0 1 2]
(3,)

[0 1 2]
(3,)

If we examine the variable explorer both values are identical and listed under value as a row vector:

When expanded we see instead both values listed as a column vector.

In essence Python automatically shows the vector as a row when the screen space in the variable is limited or a column when opened in a dedicated window and there is a lot more screen space making it convenient to use a mouse wheel to scroll through the values.

The shape for both is designated as (3,) omitting the dimension 1 as it is a single row or single column.

Specifying whether a Vector is a Row or Column

In most cases, Python picking the best layout for a vector is a matter of convenient however it can cause issues when we want to explicit state a column vector (3,1) or row vector (1,3). The function reshape can be used for this:

Python

Python

Now let’s use the reshape function to specify a as a row vector:

Python
[[0 1 2]]

Now we will see the size of arow is (1, 3) indicating 1 row (hence a row vector) by 3 columns opposed to 3 which indicated a single dimension of 3.

When opened in the variable explorer we see that arow is indeed a row:

Python
[[0]
 [1]
 [2]]

Likewise we will see the size of acol is (3, 1) 3 rows by 1 column (hence a column vector) opposed to 3, a single dimension of 3.

When opened in the variable explorer we see that acol is indeed a column:

There are also inbuilt functions r_ and c_ which can be used to convert into a row or column however these functions require the use of double square brackets and square brackets respectively.

Python
Python

Concatenation of a Vector and a Matrix

To create the matrix c we can use:

Python

If we attempt to concatenate the vector a with this matrix for instance:

\displaystyle \left[ {\begin{array}{*{20}{c}} 0 & 1 & 2 \end{array}} \right]

\displaystyle \left[ {\begin{array}{*{20}{c}} 5 & 5 & 5 \\ 5 & 5 & 5 \\ 5 & 5 & 5 \end{array}} \right]

To yield:

\displaystyle \left[ {\begin{array}{*{20}{c}} 0 & 5 & 5 & 5 \\ 1 & 5 & 5 & 5 \\ 2 & 5 & 5 & 5 \end{array}} \right]

\displaystyle \left[ {\begin{array}{*{20}{c}} 0 & 1 & 2 \\ 5 & 5 & 5 \\ 5 & 5 & 5 \\ 5 & 5 & 5 \end{array}} \right]

We will yield one of two errors:

ValueError: all the input arrays must have same number of dimensions

AxisError: axis 1 is out of bounds for array of dimension 1

To concatenate we must specify a as a row or a as a column

Python
[[0. 1. 2.]
 [5. 5. 5.]
 [5. 5. 5.]
 [5. 5. 5.]]
Python
[[0. 5. 5. 5.]
 [1. 5. 5. 5.]
 [2. 5. 5. 5.]
Advertisements

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.