Python and NumPy: Concatenation and Indexing Practice

This guide is obsolete, it was written during the early stages when I was learning python. A more up to date guide is available here:

Other python guides including installation of python are available in:

Tutorial Video

Question

Use the NumPy functions arange and reshape to create the following matrix m. Index into m to create the following coloured selections. Then recombine the selections to create the matrix n.

This image has an empty alt attribute; its file name is 9-1.png

Creating the matrix m

Python

Indexing selections from m

The yellow selection contains all rows of column 0 of m:

Python

Note how the selection is a vector and only has a single dimension with the value four. It appears as a row vector when listed in the variable explorer:

However when opened up in the variable explorer it displays as a column.

The function reshape needs to be used to explicitly specify this as a column vector. The column vector will have the original length 4 rows and 1 column.

Python

Now as you see the dimensions are updated and when viewed on the variable explorer and opened up in its own window it displays always as a column:

The red selection is along a single row, row 0 and the column is the 1st column onward.

This can be indexed using:

Python

Once again, red is a vector with a single dimension of 3. Showing as a row in the variable editor

And as a column when expanded:

This can be converted into a row vector by using reshape and specifying 1 row and 3 columns:

Python

Now in both the variable editor and when expanded it is a 1 by 3 row vector

The next selection, the magenta selection has the same form as the red selection, except it is the 1st row opposed to the 0th row:

The code for the red selection can be copied and amended

Python

This gives the following:

The next selection is the green selection.

It is the 2nd to end row and the 1st-3rd column (recalling zero order indexing is applied and we go from 0 to 3 but don't include 3).

Python

As this is a matrix already, it already is 2 by 2 and does not need to be reshaped:

The last selection is the cyan selection.

It is the 2nd to end row and the 3rd column:

Python

Once again this shows up as a vector with a single dimension of 2. It shows in the variable explorer as a row:

And when opened up in it's own window, displays as a column vector.

We can use the reshape to explicitly specify that it has 2 rows and 1 column.

Python

Now it will always show as a column vector.

Concatenation of selections to create n

To concatenate we must have a dimension that matches. The only two fragments that have a dimension to match when put beside each other in the reconstructed matrix are the cyan and green fragments. These both have 2 rows, meaning we can combine them as columns (axis=1).

Python

This gives:

Now we can look at the fragments and we can see that the red, cyangreen and also magenta fragment all have a matching number of columns. This means we can concatenate these together as rows (axis=0)

Python

Now we can concatenate the last two remaining fragments, the redcyangreenmagenta and yellow have the same number of rows meaning we can concatenate as columns (axis=1)

Python

This yields the final result.

Python