# Creating vectors and matrices

Here is how we specify a row vector in Octave:
Note that
* the vector is enclosed in square brackets;
* each entry is separated by an optional comma. x = [1 3 2] results in the same row vector.
To specify a column vector, we simply replace the commas with semicolons:
From this you can see that we use a comma to go to the next column of a vector (or matrix) and a semicolon to go to the next row. So, to specify a matrix, type in the rows (separating each entry with a comma) and use a semicolon to go to the next row.

# Operators

You can use the standard operators to
* add (+),
* subtract (-), and
* multiply (*)
matrices, vectors and scalars with one another. Note that the matrices need to have matching dimensions (inner dimensions in the case of multiplication) for these operators to work.
* The transpose operator is the single quote: '. To continue from the example in the previous section,
(Note: this is actually the complex conjugate transpose operator, but for real matrices this is the same as the transpose. To compute the transpose of a complex matrix, use the dot transpose (.') operator.)
* The power operator (^) can also be used to compute real powers of square matrices.

## Element operations

When you have two matrices of the same size, you can perform element by element operations on them. For example, the following divides each element of A by the corresponding element in B:
Note that you use the dot divide (./) operator to perform element by element division. There are similar operators for multiplication (.*) and exponentiation (.^).
Let's introduce a scalar for future use.
The dot divide operators can also be used together with scalars in the following manner.
returns a matrix, C where each entry is defined by
$C_{ij}={\frac {a}{B_{ij}}}$
i.e. a is divided by each entry in B. Similarly
return a matrix with
$C_{ij}=a^{B_{ij}}$

# Indexing

You can work with parts of matrices and vectors by indexing into them. You use a vector of integers to tell Octave which elements of a vector or matrix to use. For example, we create a vector
Now, to see the second element of x, type
You can also view a list of elements as follows.
This last command displays the 1st, 3rd and 4th elements of the vector x.
To select rows and columns from a matrix, we use the same principle. Let's define a matrix
and select the 1st and 3rd rows and 2nd and 3rd columns:
The colon operator (:) can be used to select all rows or columns from a matrix. So, to select all the elements from the 2nd row, type
You can also use : like this to select all Matrix elements:

## Ranges

We can also select a range of rows or columns from a matrix. We specify a range with
You can actually type ranges at the Octave prompt to see what the results are. For example,
The first number displayed was start, the second was start + step, the third, start + (2 * step). And the last number was less than or equal to stop.
Often, you simply want the step size to be 1. In this case, you can leave out the step parameter and type
As you can see, the result of a range command is simply a vector of integers. We can now use this to index into a vector or matrix. To select the $2\times 2$ submatrix at the top left of A, use
Finally, there is a keyword called end that can be used when indexing into a matrix or vector. It refers to the last element in the row or column. For example, to see the last column in a Matrix, you can use

# Functions

The following functions can be used to create and manipulate matrices.

## Creating matrices

* tril(A) returns the lower triangular part of A.
* triu(A) returns the upper triangular part of A.
* eye(n) returns the $n\times n$ identity matrix. You can also use eye(m, n) to return $m\times n$ rectangular identity matrices.
* ones(m, n) returns an $m\times n$ matrix filled with 1s. Similarly, ones(n) returns $n\times n$ square matrix.
* zeros(m, n) returns an $m\times n$ matrix filled with 0s. Similarly, zeros(n) returns $n\times n$ square matrix.
* rand(m, n) returns an $m\times n$ matrix filled with random elements drawn uniformly from $[0,1]$. Similarly, rand(n) returns $n\times n$ square matrix.
* randn(m, n) returns an $m\times n$ matrix filled with normally distributed random elements.
* randperm(n) returns a row vector containing a random permutation of the numbers $1,2,\ldots ,n$.
* diag(x) or diag(A). For a vector, x, this returns a square matrix with the elements of x on the diagonal and 0s everywhere else. For a matrix, A, this returns a vector containing the diagonal elements of A. For example,
* linspace(a, b, n) returns a vector with n values, such that the first element equals a, the last element equals b and the difference between consecutive elements is constant. The last argument, n, is optional with default value 100.
* logspace(a, b, n) returns a vector with n values, such that the first element equals $10^{a}$, the last element equals $10^{b}$ and the ratio between consecutive elements is constant. The last argument, n is optional with default value 50.

### Other matrices

There are some more functions for creating special matrices. These are
* hankel (Hankel matrix),
* hilb (Hilbert matrix),
* invhilb (Inverse of a Hilbert matrix),
* sylvester_matrix (Sylvester matrix) - In v3.8.1 there is a warning: sylvester_matrix is obsolete and will be removed from a future version of Octave; please use hadamard(2^k) instead,
* toeplitz (Toeplitz matrix),
* vander (Vandermonde matrix).
Use help to find out more about how to use these functions.

## Changing matrices

* fliplr(A) returns a copy of matrix A with the order of the columns reversed, for example,
* flipud(A) returns a copy of matrix A with the order of the rows reversed, for example,
* rot90(A, n) returns a copy of matrix A that has been rotated by (90n)° counterclockwise. The second argument, $n$, is optional with default value 1, and it may be negative.
* reshape(A, m, n) creates an $m\times n$ matrix with elements taken from A. The number of elements in A has to be equal to $mn$. The elements are taken from A in column major order, meaning that values in the first column ($A_{11},\ldots ,A_{m1}$) are read first, then the second column ($A_{12},\ldots ,A_{m2}$), etc.
* sort(x) returns a copy of the vector x with the elements sorted in increasing order.

# Linear algebra

For a description of more operators and functions that can be used to manipulate vectors and matrices, find eigenvalues, etc., see the Linear algebra section.
Return to the Octave Programming Tutorial index