[MUSIC]
Well we've learned how to change a matrix.
Now we're going to learn how to combine matrices to make a new matrix.
First of all we're going to explain what the combination of matrices means.
We've got an example here of two matrices, A, and B. You can see them there.
And here is a matrix C which is a combination of A and B.
C has A in its upper-left corner, B in its upper-right corner, another copy
of B in its lower-left corner, and another copy of A in its lower-right corner.
And here's what it looks like.
To make this a little easier to understand let's highlight 1, 2, 3, 4, 5, 6 in C
In red, and that's A.
A shows up in the left-hand corner.
And in the right-hand corner outlined in green, we see B.
And then there's another copy of B at the bottom left and another copy of A
at the bottom right.
So now let's see how to do this in MATLAB.
We need to ask MATLAB to make the matrices that we're going to combine.
First, let's start with a clean slate by clearing our workspace.
There, as you can see, variables are gone.
And clearing our screen.
There, nice, clean, tidy screen.
Now we’ll get those matrices.
We're going to call them A1,
them A1, A2,
and A3.
We've made these matrices
have the same number of rows and the same number of columns.
And because of that we can put them side by side or we can stack them.
Let's put them side by side. [CLICKING]
There.
And, let's stack them.
[CLICKING]
And there we have that.
Easy.
But, the only requirement for
side-by-side-ness is that they have the same number of rows.
Let's prove that, by making three matrices that each have two rows but
different numbers of columns.
And we’ll call these Bs.
B1
Equals 1 colon 1.
B2 equals
2 2, 2 2.
And B3
3 3 3, 3 3 3.
Let's look at these. -
We’ll notice that B1 has two rows. -
B2 has two rows, but it has two columns. -
B3 has two rows, but it has three columns. -
Now let's see if we can put these things on the same row. -
[CLICKING] -
Yep. -
And let's do a little more complicated: put them all there. -
And we can repeat 'em if we want to [CLICKING] -
And there you have it. - -
-
But we can't stack them. -
Let's try to do that. -
[CLICKING] -
MATLAB gets mad about that. -
It won't let you do that because the result wouldn't be rectangular. -
And arrays, as you know, matrices, have to be rectangular. -
You might be wondering about the strange word, -
“Vertcat” that popped out of nowhere. That's the name of a built-in function and, - -
as we've learned before with the plus operation and - -
the colon operation, MATLAB uses functions to carry out operations behind the scene. - -
“Vertcat”, which means vertical catenation, - -
just happens to be the function that it uses to stack matrices. - -
Okay, the rule for
stacking matrices is that the matrices all have to have the same number of columns.
You might expect us to show you an example of that, but we're going to invite you to
come up with your own examples, to demonstrate the stacking rule.
Okay, we've seen how to combine matrices.
Now we're going to change a matrix that we already have.
We're going to transform it using the transposition operator.
Here's an example of a matrix H.
It's a two by three matrix, as you can see over there on the right.
And what we're going to do is assign H prime to G.
That little character … let me make it a little, … oh, let me make it even bigger.
Right there, that little thing there
looks like a single quote in mathematics; it's usually called the “prime” symbol.
That is the transposition operator in MATLAB.
And so what does it do?
Well look at G.
First thing you notice: instead of being two by three, G is three by two.
Then you may notice that the row, first row, of H is now the first column of G.
the second row of H is now the second column of G. It changes rows into columns.
And when it does that, of course, the columns become rows.
So the first column of H is the first row of G, the second column of H is
the second row of G, and the same thing happens with the third column.
Furthermore, the indices are swapped for each element in the matrix.
By that, what I mean is, look at that two in the top.
That’s element H(1, 2). It becomes element (2,1) of G.
So the 1, 2 becomes 2, 1.
1 and 2 are swapped to become 2, 1.
Said formally, H prime, or H transpose
(m,n) is equal to H (n,m) for all m and n.
in H.
So now let's see how to do that in MATLAB.
And let's use the same matrix.
H equals 1, 2, 3; 4, 5, 6.
We want to set G equal to the transpose of H.
And here's how we do it.
Simple.
And we can see what we've done here. We have a row.
It changes to a column, and the second row changes to the second column.
Note that H itself has not changed by this operation since it's not
on the left side of an equal sign.
Let's prove that.
So here's H.
Still has two rows,
three columns. You can use transposition
to change a column vector into a row vector and vice versa.
Let's do that.
Let's set x equal to say 1; 3; 5; 7.
And now let's let x equal x prime.
This time we have changed x.
On the right side we've said we want the transpose of x, and
then we've assigned it on the left side to x.
So what was a column vector is now a row vector.
Now look at this.
[CLICKING] As you know, the column operator gives you a row vector.
Let's suppose we wanted a column vector with the same numbers.
So we naively put the prime symbol there, for transpose.
And we get the same thing.
So what happened?
Well, what we wanted to do is first make a row vector using the colon operator and
then transpose it.
But what, in fact, happened is that we transposed the five first,
and then used the colon operator.
Transposing the five does nothing because five is just a one by one matrix.
We can transpose it,
but since it's just got one row and one column, the result is the same.
To get the colon operator to go first, we have to use parentheses, like this.
Now we get what we wanted.
MATLAB will always carry out the operations inside the parentheses first.
And parentheses are important, because in MATLAB the transposition operator
is ranked above the colon operator.
Which means that without the parentheses, it goes first.
And that definitely wasn't what we wanted to happen.
So how can you find out the relative rankings of the operators?
In computer science, the rank of an operator is called its “precedence”.
And after we've had a chance to show you a few more operators,
we’ll give you a precedence table with all the rankings.
[MUSIC]
[APPLAUSE]