0:00

Now that you know how to load and save data and put your data in matrices and so

Â on, in this video I'd like to show you how to do computational operations on

Â data. And later on we'll, we'll be using the

Â source of computational operations to implement learning algorithms.

Â Let's get started. Here's my Octave window.

Â Let me just click the initialize some variables, to use for our examples.

Â Set A to be a two by two matrix, and set B to a two by two matrix, and let's set C

Â to a 2 by 2 matrix, like so. Now, let's say I want to multiply two of

Â my matrices. So, let's saw I want to compute a * c I

Â just type a * c. So it's a three by two matrix * a 2 by 2

Â matrix. This gives me this 3 by 2 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 of b.

Â So that's a * b. That's a.

Â * b. So, for example, the first first element

Â gives. One times eleven which gives eleven.

Â The second element gives 2 * 12 which gives 24 and so on.

Â So does the element wise multiplication of two matrices and in general the p grid

Â tends to, is usually used to denote element wise operations and auto.

Â So here's a matrix A and Im going to do A.iii this gives me the mul- the element

Â wise squaring of A so you know, one square is one, two square is four and so

Â on. Let's set V to a vector, let's set V as

Â 1, 2, 3 as a colon vector. You can also do one dot over V to do the

Â element Y's reciprocal of V. So this gives me 1 / 1, 1 / 2, and 1 / 3.

Â This works super matrices, so 1. / over a gives me that element y, your

Â inverse, of a. and once again the p-ratio gives us a

Â clue that this is elements wise operation.

Â You can also do things like log V, this is element wise logorithm of the of the,

Â the, V, E to the V, let's see of, base E exponentiation of v elements. So, this is

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

Â I can also do absent V, to take the element wise absolute value of V, so

Â here, you know V was all positive to aps take minus one to minus three.

Â 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 negative one times v.

Â And . What else can you do here is another neat

Â trick. So lets see, lets say I want to take V

Â and increment each others elements by one, well what we need to do is by

Â constructing a three x one. Vector this old 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 of b is 3 so 1's length would be by one.

Â This is let 1's of 3 by 1 So, that's 1's three by one.

Â On the right, and what I did was V plus one is V by one, which is adding this

Â vector of all 1s to V and so this increments V by one and you, another

Â simpler way to do that, is actually type V plus one, right, so just V and V plus

Â one also means to add to one element wise to each of my elements of V.

Â Now. Let's talk about more operations.

Â So here's my matrix A. If you want to write A transpose the way

Â to do that is to write A prime. That's the, apostrophe symbol.

Â It's the left quote. So, in, in on your keyboard you probably

Â have a left quote, and a right quote. So this is, excuse.

Â This is actually a standard quotation mark.

Â Is, type A transpose this gives me the, you know, the transpose of my matrix A.

Â And of course, A transpose. If I transpose that again then I should

Â get back my matrix A. Some more useful functions let's say

Â lowercase a us 115 20.5, so it's a one by four matrix.

Â Let's say I say vow is max of A, this returns the maximum value of A which in

Â this case is fifteen and I can do vow end max A and this returns vow and end which

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

Â So is the element number two of A that was fifteen so ends is my index in this.

Â just as a warning that you do max A. Where A is a matrix, what this does is

Â this, this actually does the column wise maximum, but I'll say a little bit more

Â about this in a second. So using this example, the variable lower

Â case, a. If I do a less than three this does the

Â element y's operation, element y's comparison.

Â So the first element of a is less than three so this gives us one, second

Â element of a is not less than three so this device is zero cause it's false.

Â The third and fourth elements of r I meant less than three, 34 elements are

Â less than three so there's just one, one, so this does the elementwise comparison

Â of all four elements of the variable lowercase A to three and the returns true

Â or false depending on whether or not it's less than three.

Â Now, if I defined a less than three, this will tell me which are the elements of a.

Â The, the variable A are less than three, and in this case the first, third and

Â fourth elements are less than three. For my next example, let me set A to be

Â equal to magic to be the magic function, returns, let's say, help magic. The magic

Â function returns functions called magics returns these matrices call magic

Â squares, they have this, yeah mathematical property that's 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, I'm just using this as a convenient way.

Â You know, to generate a 3 by 3 matrix. And, and these magic squares anyway we

Â 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 magic, I use this magic function only when I'm doing demos or

Â when I'm teaching Octave, like this and I don't actually use it for any, you know

Â useful machine only application. But let's see if I type RC equals find A

Â greater than or equals seven, this finds all the elements of A, that are greater

Â than or equal to seven. And so, RC sends row and columns.

Â So the 1-1 element is greater than seven. The 3-2 element is greater than seven and

Â the 2-3 element is greater than seven. So let's see.

Â The 2-3 element, for example, is a 2-3 is seven is this element.

Â Out here, and that is, indeed, greater than or equal to seven.

Â By the way, I actually don't even memorize, myself, what these fine

Â functions do. And what all of these things do myself.

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

Â does. And you know, I to, like, help find, to

Â look up the document. Okay just two more things and I'll

Â quickly show you, one is the sum function.

Â So here's my A I'm going to type sum A which adds up all the elements of A and

Â if I want to multiply them together I type prod, A prod sense of product and

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

Â four A rounds down these elements of A so 0.5 is rounded down to zero and seal or

Â sealing A gets rounded up to the nearest integers so 0.5 gets rounded up to 1.

Â You can also let's see. Let me type rand(3), this generates a 3

Â by 3 matrix. If I type max rand(3), rand(3) what this

Â does is it takes the element wise maximum of a two 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 randomly of, of element Y's max of

Â two randomly generated matrices. This is, this was my magic number, this

Â was my magic square three by three A. Let's say I type max A

Â and then this will be eight. Open close square brackets, comma one.

Â What this does is this takes the column-wise maximum.

Â So, the max of the first column is eight, the max of the second column is nine, and

Â the max in the third column is seven. This one means to take the max along the

Â first dimension of A. In contrast if I were to take max A, this

Â funny notation too, then this takes this per row maximum, so the max of the first

Â row is eight, max of second row is seven, max of the third row is nine and so this

Â allows you to take maxes, either, you know, per row, or per column.

Â And if you want to and remember, it defaults to a column like wise element.

Â So you won't find. If you want to find the maximum element

Â in the entire matrix A. You can type max of max of A.

Â Like so, which is 9 or you can turn A into vector and type max of A: like so.

Â And this treats this as a vector. And takes the max of, max elements of

Â that vector. 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 here's a nine by nine may magic

Â square, so let me do sum A1 so this does a per column sum so I'm going to take

Â each column of A and ad them up and this, you know let's just verify that indeed

Â for a nine by nine magic 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, comma 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 they, that, that also sums to the same thing.

Â So, what I'm going to do is construct a 9 by 9 identity matrix.

Â That's I9 and I'm going to take a and construct, you know multiply A,

Â element-wise. So here's my matrix A.

Â I'm going to do a * I9. And 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 going to do sum, sum of a of that.

Â And this gives me the sum of this, these diagonal elements.

Â And indeed, it is 369 you can sum up the other diagonal as well.

Â So the sort of top left to bottom right, you can sum up the opposite diagonal from

Â bottom left to top right. the sum, the, the commands for this is

Â somewhat more cryptic. You don't need to know this.

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

Â But let's sese flip U, flip UD stands for flip up down.

Â But if you do that, that turns out to sum up the elements and the opposites of the

Â other diagonal that also sums up to 369. Here let me show you.

Â Whereas I9 is this matrix. Flip up down of I9 you know takes the

Â identity matrix and flips it vertically. So you end up with, excuse me flip UD end

Â up with 1's on this opposite diagonal as well.

Â 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 magic, three by three magic square game.

Â If you want to invert a matrix, you type P inv A.

Â this is typically called the pseudo inverse, but it doesn't matter.

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

Â And so I can set, you know, temp equals P inv of A and [INAUDIBLE] temp times A.

Â This is indeed the identity matrix with essentially ones on the diagonals and

Â zero. It was on the off diagonals up to a

Â numerical round off. So that's it for how to do different

Â computational operations on the data matrices.

Â and after running a learning algorithm, often one of the most useful things is to

Â be able to look at your results. Or to plot or visualize your results.

Â And in the next video I'm going to very quickly show you how again with one or

Â two lines of code, using Octave. You can quickly visualize your data or plot your

Â data. And use that to better understand you

Â know, what your learning algorithms are doing.

Â