0:39

Now let's say I want to multiply two of my matrices.

Â So let's say I want to compute A*C, I just type A*C, so it's a three by two matrix

Â times a two by two matrix, this gives me this three by two matrix.

Â You can also do element wise operations and do A.* B and what this will do is

Â it'll take each element of A and multiply it by the corresponding elements B,

Â so that's A, that's B, that's A .* B.

Â So for example, the first element gives 1 times 11, which gives 11.

Â The second element gives 2 time 12 Which gives 24, and so on.

Â So this is element-wise multiplication of two matrices.

Â And in general, the period tends to,

Â is usually used to denote element-wise operations in Octave.

Â So here's a matrix A, and if I do A .^ 2,

Â this gives me the element wise squaring of A.

Â So 1 squared is 1, 2 squared is 4, and so on.

Â 2:02

And once again, the period here gives us a clue that this an element-wise operation.

Â We can also do things like log(v), this is a element-wise logarithm of the v

Â E to the V is base E exponentiation of these elements,

Â so this is E, this is E squared EQ, because this was V, and

Â I can also do abs V to take the element-wise absolute value of V.

Â So here, V was our positive, abs, minus one, two minus 3,

Â the element-wise absolute value gives me back these non-negative values.

Â And negative v gives me the minus of v.

Â This is the same as negative one times v, but

Â usually you just write negative v instead of -1*v.

Â And what else can you do?

Â Here's another neat trick.

Â So, let's see.

Â Let's say I want to take v an increment each of its elements by one.

Â Well one way to do it is by constructing a three

Â by one vector that's all ones and adding that to v.

Â So if I do that, this increments v by from 1, 2, 3 to 2, 3, 4.

Â The way I did that was, length(v) is 3,

Â so ones(length(v),1), this is ones of 3 by 1, so

Â that's ones(3,1) on the right and what I did was v plus ones v by one,

Â which is adding this vector of our ones to v, and so this increments v by one,

Â 3:52

Now, let's talk about more operations.

Â So here's my matrix A, if you want to buy A transposed, the way to do that

Â is to write A prime, that's the apostrophe symbol, it's the left quote,

Â so it's on your keyboard, you have a left quote and a right quote.

Â So this is actually the standard quotation mark.

Â Just type A transpose, this gives me the transpose of my matrix A.

Â And, of course, A transpose,

Â if I transpose that again, then I should get back my matrix A.

Â 4:25

Some more useful functions.

Â Let's say lower case a is 1 15 2 0.5, so it's 1 by 4 matrix.

Â Let's say val equals max of A this returns the maximum value of

Â A which in this case is 15 and I can do val,

Â ind max(a) and this returns val and ind

Â which are going to be the maximum value of A which is 15, as well as the index.

Â So it was the element number two of A that was 15 so ind is my index into this.

Â Just as a warning, if you do max(A), where A is a matrix,

Â what this does is this actually does the column wise maximum.

Â But say a little more about this in a second.

Â 5:11

Still using this example that there for lowercase a.

Â If I do a < 3, this does the element wise operation.

Â Element wise comparison, so the first element of A is less than three so

Â this one.

Â Second element of A is not less than three so this value says zero cuz it's false.

Â The third and fourth elements of A are less than three, so that's just 1 1.

Â So that's the element-wise comparison of all four elements of the variable a < 3.

Â And it returns true or false depending on whether or not there's less than three.

Â Now, if I do find(a < 3), this will tell me which are the elements of a,

Â the variable a, that are less than 3,

Â and in this case, the first, third and fourth elements are less than 3.

Â For our next example, let me set a to be equal to magic(3).

Â The magic function returns, let's type help magic.

Â The magic function returns these matrices called magic squares.

Â They have this, you know, mathematical property that all of their rows and

Â columns and diagonals sum up to the same thing.

Â So, you know, it's not actually useful for machine learning as far as I know, but

Â I'm just using this as a convenient way to generate a three by three matrix.

Â And these magic squares have the property that each row, each column,

Â and the diagonals all add up to the same thing, so

Â it's kind of a mathematical construct.

Â I use this magic function only when I'm doing demos or when I'm teaching octave

Â like those in, I don't actually use it for any useful machine learning application.

Â But let's see, if I type RC = find(A > 7) this finds

Â All the elements of A that are greater than equal to seven,

Â and so r, c stands for row and column.

Â So the 1,1 element is greater than 7, the 3,2 element is greater than 7, and

Â the 2,3 element is greater than 7.

Â So let's see.

Â The 2,3 element, for example, is A(2,3),

Â is 7 is this element out here, and that is indeed greater than equal seven.

Â By the way, I actually don't even memorize myself what these find functions do and

Â what all of these things do myself.

Â And whenever I use the find function, sometimes I forget myself exactly what it

Â does, and now I would type help find to look at the document.

Â Okay, just two more things that I'll quickly show you.

Â One is the sum function, so here's my a, and then type sum(a).

Â This adds up all the elements of a, and if I want to multiply them together,

Â I type prod(a) prod sends the product, and

Â this returns the product of these four elements of A.

Â Floor(a) rounds down these elements of A, so 0.5 gets rounded down to 0.

Â And ceil, or ceiling(A) gets rounded up to the nearest integer,

Â so 0.5 gets rounded up to 1.

Â You can also, let's see.

Â Let me type rand(3), this generates a three by three matrix.

Â If i type max(rand(3), what this does is it takes

Â the element-wise maximum of 3 random 3 by 3 matrices.

Â So you notice all of these numbers tend to be a bit on the large side because

Â each of these is actually the max of a element

Â 9:52

Finally let's set A to be a 9 by 9 magic square.

Â So remember the magic square has this property that every column and

Â every row sums the same thing, and also the diagonals, so

Â just a nine by nine matrix square.

Â So let me just sum(A, 1).

Â So this does a per column sum, so we'll take each column of A and

Â add them up and this is verified that indeed for

Â a nine by nine matrix square, every column adds up to 369, adds up to the same thing.

Â Now let's do the row wide sum.

Â So the sum(A,2), and

Â this sums up each row of A, and indeed each row of A also sums up to 369.

Â Now, let's sum the diagonal elements of A and

Â make sure that also sums up to the same thing.

Â So what I'm gonna do is construct a nine by nine identity matrix, that's eye nine.

Â And let me take A and construct, multiply A element wise, so here's my matrix A.

Â I'm going to do A .^ eye(9).

Â What this will do is take the element wise product of these two matrices, and so

Â this should Wipe out everything in A, except for the diagonal entries.

Â And now, I'm gonna do sum sum of A of that and

Â this gives me the sum of these diagonal elements, and indeed that is 369.

Â You can sum up the other diagonals as well.

Â So this top left to bottom left,

Â you can sum up the opposite diagonal from bottom left to top right.

Â The commands for this is somewhat more cryptic,

Â you don't really need to know this.

Â I'm just showing you this in case any of you are curious.

Â But let's see.

Â Flipud stands for flip up down.

Â But if you do that, that turns out to sum up the elements in the opposite.

Â So the other diagram, that also sums up to 369.

Â Here, let me show you.

Â Whereas eye(9) is this matrix.

Â Flipup(eye(9)), takes the identity matrix,

Â and flips it vertically, so you end up with, excuse me,

Â flip UD, end up with ones on this opposite diagonal as well.

Â 12:08

Just one last command and then that's it, and then that'll be it for this video.

Â Let's set A to be the three by three magic square game.

Â If you want to invert a matrix, you type pinv(A).

Â This is typically called the pseudo-inverse, but it does matter.

Â Just think of it as basically the inverse of A, and that's the inverse of A.

Â And so I can set temp = pinv(A) and temp times A,

Â this is indeed the identity matrix, where it's essentially ones on the diagonals,

Â and zeroes on the off-diagonals, up to a numeric round off.

Â