Introduction to Simulation: Beginner


Basic Syntax


In MATLAB there are 3 basic types of numbers that you will be working with, which are all actually matrices. There are scalars, which are just plain numbers, vectors, which are kind of like a list of numbers, and matrices, which are tables of numbers. Let's create a simple scalar:

x = 6

The variable 'x' appeared in the Workspace and if we check its size, we can see that it is a one-by-one matrix even if it looks like a scalar.
size(x)

ans =
   1   1

To create a vector, we use square brackets.
v = [1 2 3];

So, our v variable contains 3 elements and if we check its size, we get that it's a one-by-three matrix.
size(v)

ans =
   1   3

Here we used a semicolon. It we don't use it, then the result of our command will be printed out.
v = [1 2 3]

v =
   1   2   3

So, it is convenient to use semicolons when we want to keep our screen clear from too much data.

Matrices are created like vectors with the difference that rows are separated by semicolons.
A = [1 2; 3 4]

ans =
   1   2
   3   4

So, we obtained a two-by-two matrix.
size(A)

ans =
   2   2

What if we would like to combine 3 row vectors into a matrix?
P = [1 2 3 4];
Q = [5 6 7 8];
R = [9 11 11 12];

We can assign them into a matrix.
S = [P; Q; R]

S =
    1    2    3    4
    5    6    7    8
    9   10   11   12

So, we obtained a three-by-four matrix.
size(S)

ans =
   3   4


From math courses, you should know that matrices' dimensions are very important when we are multiplying them or doing other operations. And typically, you work with a column vector, not a row vector like the one that we created before. So we can easily transform it into a column vector using apostrophe to perform a transpose.

v = v'

v =
   1
   2
   3

size(v)

ans =

   3   1

So, you see that v used to be a one-by-three row vector, now it is a three-by-one vector. Of course, you can manually initialize a column vector using semicolons.
v = [1; 2; 3]

v =
   1
   2
   3


The next thing we need to learn is how to access elements of a matrix. Generally, we use parentheses and index for that. So, if we want to get an element from the i-th row and j-th column we write it like this but with numbers.
A(1,1)

ans = 1

Note that indexing in MATLAB starts from 1, not 0 as in many other languages. Another thing that we can do is to extract a vector from a matrix. So, let's say we want the first row of matrix A. First, we identify the number of the row, 1 in our case, and put a colon in the choice of the column. This means returning all the columns in row 1.
A(1,:)

ans =
   1   2

Similarly, we can use a colon syntax to get the entire column of a matrix. For example, we want to extract the second column of a matrix A.
A(:,2)

ans =
   2
   4

If we put colons in both parts, we will get the whole original matrix.
A = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 4 15 16]

A =
    1    2    3    4
    5    6    7    8
    9   10   11   12
   13    4   15   16


A(:,:)

ans =
    1    2    3    4
    5    6    7    8
    9   10   11   12
   13    4   15   16

Colon syntax can be as well used to access ranges or submatrices. Let's say we want a middle part of A.
A(2:3, 2:3)

ans =
    6    7
   10   11

If we want to get all the value from a specfic row. It is not necessary to identify the last index. So, if we write A(3,:), we'll get all elements from the third row.
A(3,:)

ans =
    9   10   11   12

The colon syntax can be used by itself, it basically creates a range. So if we do something like 1:10, it gives back all the numbers from 1 to 10.
1:10

ans =
    1    2    3    4    5    6    7    8    9   10

We can assign this range to a variable w.
w = 1:10
size(w)

w =
    1    2    3    4    5    6    7    8    9   10

ans =
    1   10

So, we can see that this command creates a row vector with the elements 1 to 10.

On the other hand, there is a command to generate linearly spaced vectors: linspace. It is similar to the colon operator (:), but gives direct control over the number of points. For example,
linspace(0, 2*pi)

ans =

 Columns 1 through 8:

        0   0.0635   0.1269   0.1904   0.2539   0.3173   0.3808   0.4443
 ...
 ...
 Columns 97 through 100:

   6.0928   6.1563   6.2197   6.2832

generates a row vector of 100 points linearly spaced between and including 0 and 2*pi.
linspace(0, 2*pi, 8)

ans =

        0   0.8976   1.7952   2.6928   3.5904   4.4880   5.3856   6.2832

generates a row vector of 8 points linearly spaced between and including 0 and 2*pi. This is useful when we want to divide an interval into a number of subintervals of the same length.

Need help? Use help command!
help colon

'colon' is a built-in function from the file libinterp/corefcn/data.cc

 -- R = colon (BASE, LIMIT)
 -- R = colon (BASE, INCREMENT, LIMIT)
     Return the result of the colon expression corresponding to BASE,
     LIMIT, and optionally, INCREMENT.

     This function is equivalent to the operator syntax 'BASE : LIMIT'
     or 'BASE : INCREMENT : LIMIT'.

     See also: linspace.