1
00:00:00,000 --> 00:00:04,753
Now that you know how to load and save
data in Octave, put your data into

2
00:00:04,753 --> 00:00:09,832
matrices and so on. In this video I'd like
to show you how to do computational

3
00:00:09,832 --> 00:00:15,042
operations on data. And later on we'll,
we'll be these source of computational

4
00:00:15,042 --> 00:00:20,190
operations to implement our learning
algorithms. Let's get started. Here's my

5
00:00:20,190 --> 00:00:26,822
active window. Let me just quickly
initialize some variables to use for our

6
00:00:26,822 --> 00:00:33,794
examples and set A to be a three by two
matrix, and set B to a three by two matrix

7
00:00:33,794 --> 00:00:39,812
and let's set C to a. Two by two matrix,
like so. Now, let's say I want to multiply

8
00:00:39,812 --> 00:00:45,550
two of my matrices. So let's say I wanna
compute A times C. I just type A times C.

9
00:00:45,550 --> 00:00:51,360
So it's a three by two matrix times a two
by2 matrix. This gives me this three by

10
00:00:51,360 --> 00:00:56,955
two matrix. You can also do elements-wise
operations, and do A. Times B. And what

11
00:00:56,955 --> 00:01:02,837
this will do, is, it'll take each element
of A, and multiply it by the corresponding

12
00:01:02,837 --> 00:01:08,288
elements of B. So that's A times B, that's
A. Times B. So, for example, the first

13
00:01:08,288 --> 00:01:13,734
el-, first element. Is one times eleven
which gives eleven the second element

14
00:01:13,734 --> 00:01:19,678
gives two times twelve which gives 24 and
so on, so the element wise multiplication

15
00:01:19,678 --> 00:01:25,766
of two matrices and in general the P rate
tends to is usually used to denote element

16
00:01:25,766 --> 00:01:31,352
wise operations in octave so here's a
matrix A and if I do A thought to carry

17
00:01:31,352 --> 00:01:37,507
two this gives me the multi-. The element
wise squaring of A so you know one squared

18
00:01:37,507 --> 00:01:43,406
is one, two squared is four, and so on.
Let's set V to a vector. We'll set v as

19
00:01:43,406 --> 00:01:49,800
1,2,3 as a column vector. You can also do
one dot over V to do the element-wise

20
00:01:49,800 --> 00:01:56,113
reciprocal of V. So this gives me one over
one, one over two, and one over three.

21
00:01:56,113 --> 00:02:02,750
This [inaudible] matrices. So one dot over
V gives me the reciprocal of one over A.

22
00:02:02,750 --> 00:02:09,246
And once again the period here gives us a
clue that this is an elements wise

23
00:02:09,246 --> 00:02:15,658
operation. It also do things like log b
this is a element wise logarithm. The

24
00:02:15,658 --> 00:02:22,323
element of the b, e to the b is base e
exponentiation of these elements. So this

25
00:02:22,323 --> 00:02:29,156
is e, this is e squared [inaudible], this
is b. And I can also do abs b to take the

26
00:02:29,156 --> 00:02:35,906
element wise absolute value of b. So here
you know, b was all positive abs -one to

27
00:02:35,906 --> 00:02:42,690
say -three. The element wise absent value
get's me back these non-negative values

28
00:02:42,690 --> 00:02:49,232
and. Negative E gives me the minus of E
this is the same as negative one times B

29
00:02:49,232 --> 00:02:56,037
but usually just for a negative B so a
negative one times B and What else can you

30
00:02:56,037 --> 00:03:01,685
do? Here's another neat trick. So, let's
see. Let's say I want to take v and

31
00:03:01,685 --> 00:03:07,631
increment each other's elements by one.
Well, one way to do it is by constructing

32
00:03:07,631 --> 00:03:13,488
a 3-by-1. Vector is all 1's and adding
that to D so they do that this increments

33
00:03:13,488 --> 00:03:20,086
D by from one two three to two three four
the way I did that was length. Of V is V

34
00:03:20,086 --> 00:03:30,339
so 1's macro V by one this is let one V by
one so let's one V by one. Alright and

35
00:03:30,339 --> 00:03:37,370
what I did was v plus one three by one,
which is adding this vector of all ones to

36
00:03:37,370 --> 00:03:43,801
v. So this increments v by one. And you,
another simpler way to do that is to

37
00:03:43,801 --> 00:03:50,833
actually type v plus one such as v and v
plus one also is to add one otherwise to

38
00:03:50,833 --> 00:03:56,388
each of my elements of v. Now. Let's talk
about, more operations. So here's my

39
00:03:56,388 --> 00:04:00,960
matrix A. If you want to write A
transpose, the way to do that is to write

40
00:04:00,960 --> 00:04:05,656
A prime. That's the, apostrophe symbol, is
the left quote. So in, in, on your

41
00:04:05,656 --> 00:04:10,416
keyboard, you probably have a left quote,
and a right quote. So this is, a, a,

42
00:04:10,416 --> 00:04:15,488
excuse me, this is actually a standard
quotation mark, 'cause, you just type A

43
00:04:15,488 --> 00:04:20,373
transpose. This gives me the, you know,
transpose of my matrix A. And of course, a

44
00:04:20,373 --> 00:04:25,731
transpose, if I transpose that again, then
I should get back my matrix A. Some more

45
00:04:25,731 --> 00:04:33,391
useful functions let's say lower case A is
11520.5. So it's a you know one-fourth

46
00:04:33,391 --> 00:04:40,060
matrix. Let's VAL = max of A this returns
the maximum value of A which in this case

47
00:04:40,060 --> 00:04:46,975
is fifteen and I can do VAL end max A and
this returns VAL and end which are going

48
00:04:46,975 --> 00:04:53,166
to be the maximum value of A which is
fifteen as well as the index so is the

49
00:04:53,166 --> 00:04:59,760
element number two of A that is fifteen so
end is my index into this just as a

50
00:04:59,760 --> 00:05:05,070
warning if you do max A. Where A is a
matrix. What this does is this actually

51
00:05:05,070 --> 00:05:10,598
does a column wise maximum. But say a
little bit more about this in a second. So

52
00:05:10,598 --> 00:05:15,900
using this example, the variable lowercase
a, if I do a less than three, this does

53
00:05:15,900 --> 00:05:21,201
the element-wise operation, element-wise
comparison, so, the first element of a is

54
00:05:21,201 --> 00:05:26,502
less than three, [inaudible] it's a one.
The second element of A is not less than

55
00:05:26,502 --> 00:05:31,870
three, so this evaluates as zero, cuz it's
false. The third and fourth element of a.

56
00:05:31,870 --> 00:05:36,669
Are of this I mean less than three
[inaudible] less than three so it's just

57
00:05:36,669 --> 00:05:41,279
one, one. So this does the element wise
comparison of all four elements of

58
00:05:41,279 --> 00:05:46,078
[inaudible] lower case a to three and it
returns true or false depend on, on

59
00:05:46,078 --> 00:05:51,256
whether or not it's less than three. Now
if I can find a less than three this will

60
00:05:51,256 --> 00:05:56,405
tell me which are the elements of A.
Therefore [inaudible] in this case the

61
00:05:56,405 --> 00:06:02,525
first third and fourth elements are less
than three. For my next example let me set

62
00:06:02,525 --> 00:06:08,719
A to be equal to magic three the magic
function returns let's type help magic,

63
00:06:08,719 --> 00:06:14,249
the magic function returns functions
called magic, returns functions called

64
00:06:14,249 --> 00:06:20,190
matrices squared, they have this your
mathematical property that all of the rows

65
00:06:20,190 --> 00:06:25,592
and columns and diagonals sum up to the
same thing so you know, is not actually

66
00:06:25,592 --> 00:06:30,731
useful for machine learning as far as I
know but I'm, I'm just using this as a

67
00:06:30,731 --> 00:06:35,539
convenient way. You know, to generate a
three by three matrix. And, and these

68
00:06:35,539 --> 00:06:40,394
magic squares had prop-, had the property
that each row, each column, and the

69
00:06:40,394 --> 00:06:45,897
diagonals all add up to the same thing. So
it's kind of a mathematical construct. I

70
00:06:45,897 --> 00:06:51,270
use magic, I use this magic function only
when I'm doing demos, or when I'm teaching

71
00:06:51,270 --> 00:06:56,514
Octave like this. And I don't actually use
it for any you know, useful machines or

72
00:06:56,514 --> 00:07:01,635
any application. But let's see if I type
RC=Find A greater than=7, this finds All

73
00:07:01,635 --> 00:07:06,475
the elements of a that I grid to equals
seven. And so, ASI sets our row in

74
00:07:06,475 --> 00:07:11,780
columns. So the one, one element is going
to seven, the three two elements go into

75
00:07:11,780 --> 00:07:17,085
seven, and the two three elements is going
into seven. So, let's see. The two three

76
00:07:17,085 --> 00:07:22,170
element, for example, is a, two, three is
seven, is this elements. Out here and that

77
00:07:22,170 --> 00:07:26,969
is indeed greater than seven. By the way I
actually don't even memorize myself what

78
00:07:26,969 --> 00:07:31,711
these fine functions do and what all of
these things do all myself. And when I use

79
00:07:31,711 --> 00:07:36,221
the fine function sometimes I forget
myself exactly what it does and you know,

80
00:07:36,221 --> 00:07:41,720
I type help find to look up. Okay, just
two more [inaudible] I'll briefly show

81
00:07:41,720 --> 00:07:47,975
you. One is the sum function. So here's my
A and type sum A. This adds up all the

82
00:07:47,975 --> 00:07:53,835
elements of A and to multiply them
together I take prod A [inaudible] and

83
00:07:53,835 --> 00:08:00,407
this returns the product of these four
elements of A. [inaudible] these elements

84
00:08:00,407 --> 00:08:06,979
of a. So 0.5 gets rounded down to zero and
ceiling A gets rounded up, 0.5 rounded up

85
00:08:06,979 --> 00:08:13,587
to nearest integer, so 0.5 gets rounded up
to one. You can also let's see let me type

86
00:08:13,587 --> 00:08:19,400
rand V this generates a 3x3 matrix if I
type max rand three rand three what this

87
00:08:19,400 --> 00:08:25,358
does is it takes the element of Y maximum
of two random 3x3 matrices. So you notice

88
00:08:25,358 --> 00:08:31,387
all of these numbers tend to be a bit on
the large size because each of these is

89
00:08:31,387 --> 00:08:37,128
actually the max of a randomly of an
element wise max of two randomly generated

90
00:08:37,128 --> 00:08:44,154
matrices. This is this is my matrix number
this is my matrix square 3x3. A, let's say

91
00:08:44,154 --> 00:08:51,228
I type max A Then this with. Open closed
square brackets comma one. What this does

92
00:08:51,228 --> 00:08:56,518
is [inaudible] column wise maximum. So
maximum of first column is eight, maximum

93
00:08:56,518 --> 00:09:01,272
of second column is nine, the maximum of
third column is seven. This one

94
00:09:01,272 --> 00:09:07,163
[inaudible] max along the first dimension
of A. In contrast if I were to take max A

95
00:09:07,163 --> 00:09:13,184
this plenty notation two then this takes
the per row maximum so the max of the

96
00:09:13,184 --> 00:09:19,434
first row is eight max of second row is
seven max of the third row is nine and so

97
00:09:19,434 --> 00:09:25,478
this allows you to take max's either per
row or per column. And if you want,

98
00:09:25,478 --> 00:09:32,152
remember it defaults to column [inaudible]
wise elements. If you want to find the

99
00:09:32,152 --> 00:09:38,415
maximum element in the entire matrix A,
you can type max of max of A, like so,

100
00:09:38,415 --> 00:09:44,842
which is nine, or you can turn A into a
vector and type max of A colon, like so,

101
00:09:44,842 --> 00:09:53,080
and this treats this as a vector and takes
the max elements of our vector. Finally.

102
00:09:53,080 --> 00:09:57,841
Let's set A to be a nine by nine magic
square. So remember the magic square has

103
00:09:57,841 --> 00:10:02,603
this property that every column and every
row sums the same thing and also the

104
00:10:02,603 --> 00:10:07,606
diagonal. So here is a nine by nine matri
magic square. So let me do sum A one so

105
00:10:07,606 --> 00:10:12,549
this does a per column sum. So I want to
take each column of A and add them up. And

106
00:10:12,549 --> 00:10:17,130
this, you know, lesson's verified that
indeed for a nine by nine magic square

107
00:10:17,130 --> 00:10:21,891
every column adds up to 369, adds up to
the same thing. Now lets do the row wide

108
00:10:21,891 --> 00:10:30,305
sum so the sum A [inaudible]. Two, and
this. Sums up each row of A and indeed

109
00:10:30,305 --> 00:10:36,149
each row of A also sums up 2639. Now lets
sum the diagonal elements of A and make

110
00:10:36,149 --> 00:10:41,670
sure that they that, that also sums up to
the same thing so what I'm going to do is.

111
00:10:41,670 --> 00:10:48,306
Constructing 9x9 identity matrix that's
I9. And gonna take A and construct you

112
00:10:48,306 --> 00:10:55,027
know multiply A elements wise, so here's
my matrix A. And to A times I9 and what

113
00:10:55,027 --> 00:11:01,834
this will do is take the element wise
product of these two matrices and so this

114
00:11:01,834 --> 00:11:09,066
should wipe out everything in A except for
the diagonal entries and now I'm going to

115
00:11:09,066 --> 00:11:15,038
do sum, sum A of that and this gives me
the Some of this these [inaudible]

116
00:11:15,038 --> 00:11:20,815
elements and indeed the 3,6,9 you can sum
up the other diagonal as well so it's top

117
00:11:20,815 --> 00:11:26,261
left to bottom right you can sum up the
opposite diagonal from bottom left to top

118
00:11:26,261 --> 00:11:31,772
right the sum of the command for this is
somewhat more cryptic you don't really

119
00:11:31,772 --> 00:11:37,085
need to know this I am just showing you
this in case any of you are curious but

120
00:11:37,284 --> 00:11:42,862
let's see. So if you so if you [inaudible]
stands up down but if you do that, that

121
00:11:42,862 --> 00:11:48,536
turns out to sum up the elements in the
opposite of the other. Diagonal feed that

122
00:11:48,536 --> 00:11:55,010
also sums up to [inaudible] here I'll show
you where as I9 is this matrix, that.

123
00:11:55,010 --> 00:12:02,377
Uptown of I9. You know takes the identity
matrix and flips it vertically so that you

124
00:12:02,377 --> 00:12:08,238
end up with excuse me [inaudible] and that
with 1's on this opposite diagonal as

125
00:12:08,238 --> 00:12:13,189
well. Just one last command and then
that's it and then that'll be it for

126
00:12:13,189 --> 00:12:18,618
[inaudible]. Lesser a to be the magic, the
magic square game and you want to invert a

127
00:12:18,618 --> 00:12:23,337
matrix you type p is a. This is
[inaudible] called a pseudo inference, but

128
00:12:23,337 --> 00:12:28,443
it doesn't matter. Just think of it as
basically inverse of a and that's the

129
00:12:28,443 --> 00:12:33,290
inverse of a. And so you can set, you
know, temp equals pn of a and of a temp

130
00:12:33,290 --> 00:12:38,009
times a. This is indeed the identity
matrix where essentially ones on the

131
00:12:38,009 --> 00:12:43,168
diagonal and zeros on the off diagonal up
to a numerical round off. So, that's it

132
00:12:43,168 --> 00:12:48,525
for how to do, different computational
operations on the data and matrices. And,

133
00:12:48,716 --> 00:12:53,818
after running a learning algorithm, often,
one of the most useful things is to be

134
00:12:53,818 --> 00:12:58,919
able to look at your results. So it's a
plot, a visualizer result. And in the next

135
00:12:58,919 --> 00:13:04,084
video, I'm going to very quickly show you
how, again, with one or two lines of code,

136
00:13:04,084 --> 00:13:08,675
using [inaudible], you can quickly
visualize your data or plot your data.

137
00:13:08,675 --> 00:13:13,777
And, use that to better understand, you
know, what your learning algorithms are
