[MUSIC] Hi, welcome to lesson two,
matrices and operators. As we pointed out in lesson one, MATLAB is
designed not only to make it easy to program a computer but also to make it easy to
solve numerical problems. Numerical problems often involve operating
on matrices, and MATLAB makes that easy. We're going to start this lesson
by finding out what matrices are. Matrices, which is the plural
of the word “matrix”, are special kinds of arrays.
So what are they? Well, both concepts are pretty simple,
really. First of all, an array is just a set of
numbers arranged in a rectangular pattern. So imagine a piece of paper with rows of
numbers written on it, like this one. This is a two-dimensional array. The numbers are called elements of the array,
and each row must have the same number of
elements as each of the other rows. In this case we have six
rows of four elements. We call this a six by four array. As you can see it has six rows and
four columns. And we always say the number of rows
before we say the number of columns. Now imagine having a stack of two such
pages, each with the same dimensions but not necessarily the same numbers. We use the same two-dimensional
array that we started with for one page, and we add a second page
with the same layout as the first one, that is six rows and four columns. Now we have a three-dimensional array. Its dimensions are six by four by two,
which means six rows, four columns, And two pages. We always say the number of pages after
the number of columns just as we always say the number of columns after
the number of rows. Now let's add a third page with the same
layout as each of the other two pages. This is still a three-dimensional array.
We've just changed the dimensions. This one is 6 by 4 by 3,
which means 6 rows, 4 columns and 3 pages. Arrays of four dimensions and
higher are rare. We won't use them in this course. And the most common
are the two-dimensional arrays. In fact, they're so common,
they have a special name. A two-dimensional array is also called
a matrix, and that's what we call them. And if there's only one row in the matrix,
or only one column, in other words it's only one-dimensional,
the matrix is also called a “vector”. The most ingenious part of MATLAB is the
way Moler set it up to deal with matrices, and because of the special role
that matrices play in MATLAB, he gave his language a name that
stands for “Matrix Laboratory”. Okay let's look at some
matrices in MATLAB. Before we look at a matrix, let's remember what we did in
Lesson One when we were doing plotting. We actually made a vector called
x_values and we did it this way. [CLICKING]. We used a left and right bracket
to mean that we wanted a vector, in this case of three values. We're going to use those same brackets
to make a two-dimension array, and here's how we'll do it. We can use capital X this time. Here's my left bracket,
zero, one, minus one. This time I'm going to put a semicolon. That means end of a row. I want to put another row. It has to have the same
number of elements. Pie, one hundred, right bracket
means the end of the matrix. And what we see here is
we have a 2 by 3 matrix. First row is zero, one, minus one. Second row is 2.5, 3.1416, and 100. Let me say a word about the 3.1416. We actually typed pi here, and
as you probably guessed, pi means the ratio of the circumference
of a circle to its diameter. You might think that pi is some
kind of built-in variable. It's not. It's a built-in function. As you remember from lesson one, a function is an operation that's
invoked by giving its name. And we had examples there of clear, plot,
title, axis. These are functions that
did things to the screen. Pi is a little different. What it does is return a value, the ratio of the circumference
to the diameter of a circle. It actually returns a lot more
decimals than we're seeing here. The format we have set up only shows one,
two, three, four, five digits, as you can see. So pi returns a value. And, when we say it returns
a value, we simply mean that the program inserts the value, where the function name appears.
As you can see, the function name appeared in the middle
of the second row, and so the value appears in
the middle of the second row. MATLAB provides hundreds
of built-in functions. Here's one call squirt. It actually means
square root. MATLAB loves short names. And you can see when we hit return that we get the value 1.4142
which is the square root of two. Of course both of these numbers, pi and
square root, are irrational numbers. So these digits would go on forever. MATLAB stores about 15 of them. And it's showing you just a few. Here's another function: sine of 30; sin like square root
takes an input value, pi takes no input value. Some
functions do; some don't. You might think this
is sine of 30 degrees. It's actually the sine of 30 radians. If you want the sine of 30 degrees
you can do it this way, sine d. Looks like sinned. And that's the sine of 30 degrees. Here's another function: size. I'm going to give it an argument: x. It says two three. What this means is: X is a two by three matrix. The job of size in life is
simply to give the dimensions of the matrix that you
give it in parentheses. Let's try this with another value. Let's make x equal five. And let's do the size of x. Now x is just a simple number, and since size is supposed to give you
the dimensions of a matrix and x clearly is not a matrix, it's
probably going to give an error, one of those red messages. Well, no, it doesn't. In fact, it gives us that x
has the dimensions one by one. And here we see that MATLAB
looks at everything as a matrix. This
is one of the ingenious ideas that Cleve Moler had
to begin with with MATLAB. A scalar is simply a one by one
matrix in the eyes of MATLAB. You noticed that we used a lower
case x for this one by one matrix. It's commonly called
in mathematics a “scalar”. And it's common to use lowercase as the initial value or initial letter in
the name of a vector, as we can see from xvalues up here at the top of the screen
and x as we did here for the scalar. And we used a capital X for
a two-dimensional array. MATLAB knows nothing about that.
It's just a custom. You might, say, maybe, we want
an array of voltages. [CLICKING] Let's give it one,
two, three, four, five, six. I'm in a hurry. I don't want to come up with any fancy values. So there's a three by
three array of voltages. If we wanted to say what the first
row was, we might say row one, and I’ll use an underscore to
make this clear, of voltages equals, well, one, two, three. And I used a lower case r,
because this is after all a vector. Now let's look at a vector
a little bit more closely. Let's set x equal to one, four, seven. This is a row vector.
Let's look at a column vector. I'm going to set y equal to
1 semi-colon 4 semi-colon 7. Since semi-colon means the end of a row
we've got a row of only one element, another row of only one element and a third row of only one element. And as you can see row one is one element, four on the second row . . . So this is a column vector, and MATLAB lays it out as a column vector
when it echoes back the value of y. So let's look at the size of x and the size of y. For x we have a one by three. That means one row, three columns. For y we have a size of three by one:
three rows, one column. X is a row vector. Y is a column vector. MATLAB doesn't use these names. We use
the names, but MATLAB shows you that it's a vector by showing you that the size
has the number one in it somewhere. A vector is simply a matrix,
one of whose dimensions is one. And a scalar is a vector. It's a matrix both of whose dimensions,,
are equal to one. [MUSIC]. Let's look at a picture from a book that
shows the relationship among all these types of arrays. Here we see all four types, the arrays,
the matrices, the vectors, and the scalars. First we highlight the set of all arrays. This is the biggest set and it includes all numbers of dimensions,
one, two, three, four, and on and on. In particular, it includes as
subsets the special types of arrays, the ones with other names. Here are the matrices. You can call these matrices or arrays,
and inside the matrices are the vectors. This set includes all row vectors and
all column vectors. And you can call them vectors
or matrices, or arrays, but we usually call them vectors. And, finally, inside the set
of vectors are the scalers. These are the one-by-one matrices. And you can call them scalers or
vectors, or matrices, or arrays. But we usually just call them numbers. Every thing that the MATLAB works
with from the largest to the smallest is found in this diagram. And on the subject of size,
now we are going to see how you can create a very long vector without
a lot of typing by using the colon operator. [MUSIC] [SOUND] [APPLAUSE]