Welcome to this matlab tutorial. This
matlab tutorial is going to be on the
basic syntax of matlab. So I've already
opened matlab and this is generally the
screen that you see most of the time. So
the big main window's called the command
window, you'll be working here most of
the time. You can view your variables in
the workspace on the bottom left, and you
can view your current directory on the
top left, and so that will tell you where
in the file system you currently are. So,
if you want to open a file from there
you don't have to type in the absolute
path you can just type in the filename
directly, or if you save a file that's where
it will go. So we call that the working
directory and you can change the working
directory to keep all your projects
separate. So, you can go into the path up
here and you know type in another path
if you want to use the path for your
particular project. So in matlab there are
three basic types of numbers that you'll
be working with, or variables, that are
all actually matrices. So you have
scalars which are just plain numbers,
you have vectors which is kind of like a
list of numbers, and then you have matrices
which are tables of numbers. So as I've
mentioned before in matlab all these
variables are represented as matrices. So
if I do something like x = 5 you
see that in a regular programming
language you might just think of this as
x = 5,
but I can do something like check the
size(x) and I see that it's actually a
1 by 1 matrix. I can create a vector,
we use square brackets, and you don't
need commas but i'm going to use them
and so that creates a vector with the
v = [1,2,3];
So if I do size(v) it's a
one by three vector, or you can also call
it a one by three matrix. So this brings
up some other interesting points about
how matlab syntax works. I mentioned that
you don't actually need the commas so we
can just do that and matlab knows that
we want one by three matrix with the
elements 123.
So you also notice that I added this
semicolon at the end of the line and
what that does, it's easier to see if I
just don't do it, so it prints out the
last thing. So if I don't use the
semicolon it's going to print everything
out from the previous command, but if I
do use the semicolon it will suppress
the output. So, that's why I didn't put a
semicolon after size(v). If I did I
wouldn't see anything.
Now we're going to go on to look at
matrices, So it's very similar to
creating a vector except we need to have
a way to separate each row, so that's
done with a semicolon and I'm not going
to put one at the end so we can see the
output.
And so what that does is it creates a
two by two matrix now with the elements
1234 in the variable A, and if I do size(A) it's two by two.
So those are the two things we've
learned about initializing vectors and
matrices that you can use commas to
separate columns but you don't need the
commas you can just use spaces, and to
separate rows you can use semi-colons.
Another thing is that when you're
working with matrices and vectors
and you're multiplying them and stuff
like that, the dimensions of the vector
are very important. So, typically you know
you work with a column vector not a row
vector like the one I just created, and
so we can easily transform the previous
vector which was a row vector into a
column vector using an apostrophe and so
that does a transpose. And so you see now
that v used to be a one by three vector
and now it's a three by one column
vector. You could have also initialized
it using semicolons of course.
Ok, so the next thing I want to talk
about is how you can access elements of
a matrix. Typically that's done using
parentheses and then indices and commas
in between. So if I want to
in general access the i row in the j
column it would be
a(i,j) So if I want a(1,1)
for example I would do that.
If I want a(1,2) and so on a(2,1), Another
interesting thing you can do is access a
vector within a matrix. So let's say I
want the first row of the matrix a, I can
use the colon syntax to get the first
row. So I'm going to put a 1 where I
choose the row, and then I'm going to put
a colon in the choice for column which
means return me all the columns in row 1.
So if I do that I get 1 2 which is the
first world of A. Similarly I
can use the colon syntax to get an
entire column of A. So let's say I want
to choose the second row, or sorry the
second column, so let's say I want to
choose the entire second column, I would
do that.
And of course if I do a colon in both
parts of A it will just return me the
entire A. So let's create a bigger
matrix now so I can better illustrate my
next point.
Alright, so I just created a four by four
matrix. So the colon syntax can also be
used to access ranges. So if I want
I look at A it's 1-16. If I want the
part of A that has so a sub matrix that
has the 6, 7, 10, 11 I could
access that part by choosing the second
and third row in the second and third
column. If you've programmed before usually
when you're working with vectors,
matrices, and arrays they're zero indexed, in
MATLAB it's one indexed. So, a 1-1 returns me
the first element, it's not a 0-0. Another
cool thing that we may use later is the
colon syntax can be used all by itself
it actually just creates a range. So if I
did something like 1:10 gives me
back all the numbers from one to ten, and
so say I want to assign this to a
variable, let me check the size of W, so
that creates a row vector with the
elements 1 to 10. It's simpler
to do than typing one
through ten manually, right. So just to
extend this a little bit we aren't
limited to only scalars, vectors, and
matrices in matlab. You may have heard of
a data structure called tensors, and so
those are multi dimensional matrices, so
it's not a table it's more like a cube
or a hypercube.
So you can instead of having a
two by two matrix you can have a two by
two by two tensor.