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:

Table of contents

- Tutorial Video
- Perquisites
- Note for MATLAB/Octave Users
- Zero-Order Indexing of a Vector
- Zero-Order Indexing of a Matrix
- Indexing Multiple Elements
- Selecting an Entire Column or Row
- Selecting Every nth Value of a Matrix
- Transpose of a Matrix
- Concatenation of Matrices
- Transpose of a Vector
- Specifying whether a Vector is a Row or Column
- Concatenation of a Vector and a Matrix

## Tutorial Video

## Perquisites

## 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.

To create this in MATLAB one would use:

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:

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:

`[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:

```
[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:

We can index a single value using square brackets:

`9`

`12`

We can also index negative values:

`12`

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

`[4 6]`

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

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).

Using a colon alone we can select every single value:

`[ 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:

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

```
[ 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.

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

`6`

`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:

`[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:

`[4 5 6]`

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

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

`[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:

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

```
[[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:

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

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

## Concatenation of Matrices

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

```
[[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:

```
[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:

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

`[[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:

```
[[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.

## Concatenation of a Vector and a Matrix

To create the matrix c we can use:

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

To yield:

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

```
[[0. 1. 2.]
[5. 5. 5.]
[5. 5. 5.]
[5. 5. 5.]]
```

```
[[0. 5. 5. 5.]
[1. 5. 5. 5.]
[2. 5. 5. 5.]
```