Skip to content

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:

a = 1
b = 2
c = a + b

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:

a = 1;
b = 2;
c = a + b;

You can still see the the values of the variables by typing their names, such as by typing:

c

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.

a = [1 2 3 4];
b = a + 1

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.

b = a'

Sometimes you need to be careful with spaces when defining arrays. For example, try copy/pasting the following:

c = [1 - 2]
c = [1-2]
c = [1 -2]
Often, commas are used to separate the elements when defining vectors:
c = [1, - 2]
c = [1,-2]
c = [1, -2]

Another way to define a column vector is using semi-colons:

b = [1; 2; 3; 4]

Note that you don’t need to define arrays by typing numbers. For example, you could define a vector by:

a = 1;
b = 2;
c = 3;
x = [a; b; c; b+c]

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:

C = [1, 2, 3
     4, 5, 6]
Alternatively, you can separate the rows using semi-colons:
C = [1, 2, 3; 4, 5, 6]

The size of a matrix can be determined using the size function.

num_rows = size(C,1)
num_columns = size(C,2)
size_c = size(C)
Notice that the names of the variables in the above snippet consist of several characters. There is flexibility in the naming of variables. You can use upper and lower case letters, as well as underscores (_). 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:

D = [C [0; 0]; [0; 0] C]

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:

d23 = D(2,3)
You can also extract the entire 2nd row, or 3rd column by:
row2 = D(2,:)
column3 = D(:,3)

Alternatively, you could pull out a submatrix from D by:

submatrix = D(3:4,2:4)
In the above, 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:

D(5) = 10;
disp(D)

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:

Z = zeros(2,3)
Alternatively, it can be called by:
Z = zeros([2 3])

Ones

Similarly the ones function can be used to create an array of ones:

O = ones(2,3)

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:

r = 1:10
If we want this as a column vector, then we would need braces in order to transpose it - otherwise the transpose would just apply to the 10.
r = (1:10)'

Note that the values don’t need to increment by 1. Other increments could be used. For example, try the following:

r = 1:0.5:5
r = 5:-0.5:1

Diag

A matrix with values on the diagonal, but zeros elsewhere could be generated by:

D = diag(1:4)
This same function can be used for pulling out the values along the diagonal of a matrix:
d = diag(D)

Eye

Sometimes an identity matrix is needed. This is a matrix of zeros, but with ones on the diagonal:

I = eye(3)

Kron

An occasionally useful function is kron, which does a Kroneker tensor product. For more information about this (or other MATLAB functions), type:

help kron

A simple example of its use might be:

kron(eye(3),ones(4,1))
Note that in the above expression, no output variable name was specified. This causes the output to be saved in a variable called ans.

Rand and Randn

Arrays of uniform random numbers can be generated, with values between 0 and 1, by:

R = rand(5,3)

Normally distributed random numbers, with mean zero and variance one, can be generated by:

R = randn(5,3)

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:

save my_workspace.mat

Alternatively, a few selected variables can be saved by:

save saved_variables.mat R d

Sometimes it is useful to save an array into a text file

save -ascii R.txt R
MATLAB variables can be removed using the clear command. If needed again, files containing MATLAB variables can be loaded back into the MATLAB workspace by:
clear
load saved_variables.mat
or:
R_restored = load('R.txt','-ascii')