MATLAB Variables¶
Once MATLAB is started, you can type into the main window to define and manipilate variables.
Scalars¶
The simplest types of variables in MATLAB are just numbers, which you can work with. For example, try typing the following into MATLAB:
Notice that MATLAB has shown you the answer each time you type a line. You can prevent this by putting a semi-colon at the end of each line, as in:
You can still see the the values of the variables by typing their names, such as by typing:
Vectors¶
Sometimes you might want to work with arrays of numbers. For example, a greyscale image is simply a 2D array of numbers. The simplest arrays are vectors, which are 1D arrays.
In the above, a
and b
are row vectors.
You can convert a row vector into a column vector by transposing it with the '
operator.
Sometimes you need to be careful with spaces when defining arrays. For example, try copy/pasting the following:
Often, commas are used to separate the elements when defining vectors:Another way to define a column vector is using semi-colons:
Note that you don’t need to define arrays by typing numbers. For example, you could define a vector by:
Matrices¶
Matrices are 2D arrays of numbers. A column vector with \(N\) elements can be thought of as a \(N \times 1\) array, and a row vector with \(N\) elements as a \(1 \times N\) array. In MATLAB (unlike some other programming languages), a scalar is simply a \(1 \times 1\) array. You can define a matrix by, for example, typing:
Alternatively, you can separate the rows using semi-colons:The size of a matrix can be determined using the size
function.
_
).
You can also include numbers in the name, providing the variable name does not begin with a number.
You can define matrices using other matrices, for example by:
The elements from within a matrix can be read according to the indices of the values that you want to read.
Unlike Python, indexing starts at 1, rather than 0.
Reading the value in the 2nd row and 3rd column of D
can be done by:
Alternatively, you could pull out a submatrix from D
by:
2:4
means the values from 2 to 4, (i.e. 2, 3 and 4).
If a matrix is indexed by a single number, then it gets treated as a vectorised version of itself, where all the columns are concatenated together. For example, try:
Other ways to create arrays¶
There are various other methods for creating arrays and matrices. Some of them are listed here.
Zeros¶
The zeros
function can be used to create an array of zeros:
Ones¶
Similarly the ones
function can be used to create an array of ones:
Colon¶
We saw above that expressions with colons in them, such as 2:4
, can be used for indexing, but these can also be used to create arrays:
Note that the values don’t need to increment by 1. Other increments could be used. For example, try the following:
Diag¶
A matrix with values on the diagonal, but zeros elsewhere could be generated by:
This same function can be used for pulling out the values along the diagonal of a matrix:Eye¶
Sometimes an identity matrix is needed. This is a matrix of zeros, but with ones on the diagonal:
Kron¶
An occasionally useful function is kron
, which does a Kroneker tensor product.
For more information about this (or other MATLAB functions), type:
A simple example of its use might be:
Note that in the above expression, no output variable name was specified. This causes the output to be saved in a variable calledans
.
Rand and Randn¶
Arrays of uniform random numbers can be generated, with values between 0 and 1, by:
Normally distributed random numbers, with mean zero and variance one, can be generated by:
Loading and Saving¶
All the variables in the MATLAB workspace can be saved to a specified file (in this case it is called “my_workspace.mat”) using:
Alternatively, a few selected variables can be saved by:
Sometimes it is useful to save an array into a text file
MATLAB variables can be removed using theclear
command.
If needed again, files containing MATLAB variables can be loaded back into the MATLAB workspace by:
or: