0:06

Hello everyone.

Â We are now onto the third part of our lecture on

Â how to use the MATLAB

Â programming environment to form scientific computations.

Â So this is computing with MATLAB, part three.

Â In some ways this is going to be a continuation of the previous

Â lecture, where we discussed strategies for

Â using MATLAB for data manipulation and analysis.

Â But we're going to have a different focus compared to that last lecture.

Â In this lecture, we're going to discuss several useful

Â MATLAB built-in functions, such as those you can

Â use to compute the mean, the maximum or

Â the standard deviation of data that you may have.

Â And we're going to particularly focus on the way that you can

Â use MATLAB to determine the array locations that fit particular criteria.

Â We'll get to that a little bit later, but what I mean by that is sometimes you don't

Â care about the maximum value itself, sometimes you care

Â where in your array does the maximum value occur.

Â We're going to talk about how you can use that functionality of MATLAB.

Â We're also going to discuss strategies for using if statements and

Â then we're going to talk about exporting and importing results in MATLAB.

Â 1:12

So, several useful MATLAB built-in functions can be summarized as follows.

Â Let's say that we have a one by five array, little a equals these five numbers.

Â one, five, two, four, three.

Â We can compute little b equals the mean of a.

Â And for all the numbers one through five, the

Â mean value in this case is going to be three.

Â That computes, mean computes the average.

Â We can compute little c equals std of a.

Â 1:49

Or we could say, little d equals the max

Â of a, that's going to give us the maximum value

Â in this one by five array, which in this

Â case is, of course, going to be equal to five.

Â Or you could say, little e is equal to the sum of a.

Â That's going to sum all the values one through five,

Â which is going to give you a value of 15.

Â Now if we said little f equals the square root of

Â A, in this case we're not going to get a single

Â number like we did with these other functions, we're going to

Â get another one by five array, with these five numbers here.

Â So what this function is doing, is computing the

Â square root, but it's doing it on an element-by-element basis.

Â 3:29

So what this is doing is your first, the first number, the first

Â variable name you give it, D, is what's holding the maximum value itself.

Â And the second variable you give it, index, is

Â what's holding the location of the maximum in the array.

Â So the reason this is two is because you can see where does the maximum occur.

Â Well the maximum occurs here, which and, in this

Â case, five is the second element of our array a.

Â And a little bit later we'll see how this functionality of MATLAB can be used.

Â 3:57

A sort of related command here is if we say j equals find a greater than two.

Â That's going to give us this result.

Â j is two, four, five.

Â Why do we get these three numbers?

Â Well, what find does is, it returns the indices of all the

Â elements of your, of your array, where a certain condition is true.

Â So, in this case, the condition you're asking is, where is a greater than two?

Â So it finds all the elements in a that have a value greater than two.

Â And so you can look, and you can say okay, the value one is not

Â greater than two, therefore we don't have the one here, this is the first element.

Â The value five, which is our second element, which has an index of two.

Â Five is greater than two, so therefore we get a two.

Â And, four is greater than two, so the fourth element

Â here, is you get four because that's the forth element.

Â And then this, number three is also greater than

Â two, and three in this case, it's the fifth element.

Â So the second element, the fourth element, and the

Â fifth element of A are all greater than two,

Â so this is what the Find command does, is

Â it returns all the indices where a certain condition holds.

Â 5:04

Now what if A, instead of using little A as a one by five, array

Â or a vector, what if we had capital A and we had this as a three by three array.

Â With something like this, these, this set of nine numbers arranged three by three.

Â If you said K equals max of A, most people would look at this

Â three by three array and say K is going to be equal to even.

Â Because K is the largest value, in this three by three array.

Â In this case, the way the MATLAB

Â interprets the max command, this isn't exactly true.

Â K in this case, is going to be a one by three array, three, five, seven.

Â So what MAX does, is it computes the maximum on a column by column basis.

Â You see, three is the maximum value in this column.

Â Five is the maximum value in this column.

Â Seven is the maximum value in this column.

Â But what if you didn't want to do this on a column by column basis?

Â What if what you really cared about was

Â what is the overall maximum value in this array?

Â What if you really wanted to get seven out of this.

Â Well, you could just say little L is equal to the maximum of the maximum of A.

Â In that case, it would it would give you seven.

Â So first, it would compute this one by

Â three array, and then it would say, okay, out

Â of this one by three array, with numbers three,

Â five, and seven, which of those is a maximum?

Â And in that case, of, you're going to get of course, the number seven.

Â So this one, this is the way of returning the overall maximum of your array.

Â 6:29

So one thing we mentioned in the last slide is,

Â when you use the max command or also the min

Â command, you can have MATLAB tell you not just the

Â maximum value but also where in your array that maximum occurs.

Â And here's a practical example of how you could use this.

Â What if you wanted to find out where our curves cross?

Â You had y one, which is the curve here which increases to the function of x.

Â And then you had y two which is the

Â red curve which decreases to the function of x.

Â Now of course mathematically the curves cross where y one equals y two.

Â So all you would have to do is say well, let me

Â compute y one minus y two and find where that's equal to zero.

Â But what if the values are never exactly equal, right?

Â I mean, this is true in a formal mathematical sense but when

Â you're looking at real data, like, real data are not defined continuously.

Â Real data are defined in individual, data points.

Â So, you could have your blue, which would be, you know,

Â these dots here and your red would be these dots here.

Â And you can see that they never exactly overlap.

Â 7:41

You could say, dummymin comma index equals the minimum

Â of the absolute value of Y one minus Y two.

Â So, this is a way to compute, when does our, my, subtracted.

Â When does my difference vector y one minus my, y two get closest to zero?

Â So you want the minimum but you want the minimum to be absolute value.

Â So that's when this number is, when this difference y

Â one minus y two is going to be closest to zero.

Â And then you can say the crossing point in this case is equal to x sub index.

Â And then you can plot x sub index versus y

Â one sub index in green, and this point here would appear.

Â This is where, this is the point that it would

Â it would identify, is where the two curves cross one another.

Â So, let's review why we did it, this way.

Â The reason we made this a dummymin is because

Â this is a value we don't really care about.

Â We don't care in fact how close these two curves come to one another, right?

Â They could be off by 0.01 or they could be off by 0.01 or 10 to the minus seven.

Â We don't really care exactly about the numbers.

Â We just care, where is it the closest, right?

Â What you care about is what's the value of x at

Â this point, where the two curves come closest to one another.

Â So, what we care about in this case

Â is the index, not the actual minimum value itself.

Â 9:45

Now a little bit more on, on built-in MATLAB functions.

Â You can combine these functions with

Â concatenation, like we talked about before.

Â So if we typed in little a equals

Â 1:10, put parenthesis around that and put an apostrophe.

Â Remember from previous lectures that apostrophe means transpose.

Â So that's going to take the numbers one through 10 in a row vector and

Â apostrophe is going to transpose it, so it's going to convert that into a column.

Â Then you can say something like big A is little a comma, square root of A.

Â 10:18

A squared, comma, sin A, and you would get this

Â ten by four array here, where the first column is A,

Â the second column is the square root of A, third

Â column is A squared, fourth column is the sin of A.

Â There's a little twist here.

Â When we computed A squared, you noticed that we put a dot here, right?

Â We computed a dot carat two, this is saying

Â take a to the second power, but not exactly.

Â It's saying take each element of a times

Â the corresponding element of a, so times itself.

Â 11:10

And, note, there's, if you wanted to compute this particular

Â ten by four array, there's different ways to do it.

Â You could do it like this.

Â You could say, little A equals 1:10 with ()

Â around it, and then an ; to indication transpose.

Â And then you could say big A:,1 is A, big A :,2 equals square root of A, etc.

Â Remember, this is a way that you can access the entire first column

Â of A, the entire second column of A, the entire third column of A.

Â 11:58

So now let's discuss how you can read in and write out results in MATLAB.

Â Probably the most useful commands are what are called save and load in this case.

Â Let's say that you're typing in a bunch of commands

Â in MATLAB, you had defined a bunch of variables and

Â then you had to go home for the day or

Â for whatever reason you had to turn your computer off.

Â Maybe you're, you're laptop was running out of batteries.

Â But you want to come back to it later, you want, you wanted to

Â sort of stop for a while and then come back exactly where you left off.

Â Well, the simplest thing to do is just to type save matlabsession.

Â That's going to create a file in whatever directory you're in that's called mat,

Â it's going to have the name matlabsession, with the, extension .mat.

Â And then later, if you go into that

Â directory, you can go to the MATLAB command prompt

Â and then type load matlabsession, and that's just going to

Â read in all your variables that you had before.

Â So this is a, this creates a type of file,

Â a .mat file, it can only be read by MATLAB.

Â It can't be read by other, by other programs,

Â but it is very useful in the sense that

Â it will just save all the variables that you

Â had, everything that you had done up to that point.

Â And then when you type, when you load that

Â back in you can just start wherever you left off.

Â 13:18

So, if we read, if we typed something called

Â sample script two and that created an A and lets

Â just say for the sake of argument sample script two

Â creates an array capital A with size 100 by 10.

Â Then we can type this, dlmwrite

Â parenthesis, apostrophe A dot dat end apostrophe, comma A,

Â comma, and then, apostrophe, backslash T, apostrophe, then end parenthesis.

Â This is going to create a file called A dot dat.

Â And what this is telling that letter to do,

Â DLM rate, in this case, stands for the limited, right.

Â So, this is going to create what's known as a tab-delimited text file.

Â The reason we know that it's a tab-delimited text file

Â is because of this little thing here, this backslash t.

Â This says, take all your variables in A and separate them by, by tabs.

Â The default would be to separate them by commas, but sometimes separating

Â them by tabs is, makes them a little bit easier to read.

Â So the three things that we're telling dlmwrite in

Â this case are, first of all, the file name, A.dat.

Â Secondly, what variable to put into that file, which

Â in this case, the variable we want to put in that

Â file is capital A, and then this third thing

Â is telling them to separate the values by backslash t.

Â What's useful about this is now your, your

Â file A.dat, you can read that into Microsoft Word.

Â And you can see what the numbers are.

Â You could also read it into Excel if you

Â wanted to perform more computations on this using Excel.

Â And also standard software packages that are used for things

Â like plotting can read these, these tab limited text files.

Â 14:55

Or later if you wanted to read this in mat

Â lab, you could type dlm read at the MATLAB prompt.

Â So just like you have a file called dlmwrite

Â which is a way of exporting tab delimited text

Â files dlmread is a way of importing a tab

Â delimited text file or a comma delimited text file.

Â So you could write a.dat then you could

Â read it in later using this command called dlmread.

Â And then, a third way that you can, ex,

Â re, import and export data in MATLAB is through

Â images there's a file called imread for image read,

Â sorry there's a function called imread for image read.

Â There's also a function called imwrite for image write.

Â They can read in standard formats like JPEG and TIFF, PNG files et cetera.

Â 16:04

So let's go through an example for how we could, how we might use an if statement.

Â Let's assume that we had a one D array called little a.

Â And all the values in this one-D array should, in

Â principle, be positive, or at least they, they should be non-negative.

Â They should be either positive, or they should be equal to zero.

Â So what we want to do is we want to go through our one-D array a, and every

Â time we see a negative value, we want to alert the, the user, so you know about it.

Â You know, hey, that we have a negative

Â number here where we're not supposed to be a

Â negative number, and then what we also want to do

Â is we want to set the value equal to zero.

Â So, these commands would, would do what we need, in this case.

Â We could say for i equals one, up to the length of a.

Â Like we discussed before, this four loop is going to start with a value

Â of one, and it's going to come up to the value of length of a.

Â So it's going to go through all the elements of a.

Â And then what we want to say is, if a sub i

Â is less than zero, then we use this command called disp.

Â Disp in this case is short for display.

Â And so this is going to put more text down on the screen.

Â And the text is going to say is, a negative value

Â is found at a particular index and in order to

Â print the index number on the screen, we use this

Â command that we encountered previously, int to string sub i.

Â So what this is going to do is print out on our screen.

Â Negative value is found at index six, or index 14, or index 21, or whatever.

Â And then what we want, the other thing we want to do is, every time that a

Â sub i is less than zero is, we want to set a sub i equal to zero.

Â And then we have an end command, here, this

Â end is going to signify the end of the if statement.

Â And then because this is in a for loop you need to have

Â a second end command here which signifies the end of the for loop.

Â So you can probably see what how the if statement is useful in this case.

Â We don't want to set all of our values of a equal to zero, right?

Â The only time we want to set the value of a equal to

Â zero, is if that particular value of a was was negative to begin with.

Â If it was positive, like it was supposed to be, we don't want to do anything.

Â So, this if command will only execute these two, if, if, statement

Â is set up so that it will only execute these two commands.

Â It will only display this message and it will only set the value of A equal

Â to zero if that corresponding value of A sub I is less than zero to start with.

Â If that value of A sub I is equal to zero or it's greater than

Â zero, it's just going to skip over this and

Â nothing is going to happen in this case.

Â 18:30

So a couple of notes on this.

Â One is that there are easier ways to test these sorts of things.

Â Some of you may be familiar with programming logic

Â or some of you may be familiar with MATLAB already.

Â And if you're saying to yourself there's an easier way to do

Â this then, yes, you're right, there's an easier way to do this.

Â This is just meant to illustrate how the if statement works.

Â And a second note here is, we've

Â encountered a couple of conditional statements like this.

Â If something is less than zero or greater than zero or greater than another value,

Â et cetera, what if we wanted to say, you know, is a exactly equal to zero?

Â Is, is one value of a exactly equal to zero?

Â Well, in that case, you have to use a

Â double equals sign because you need to specify to MATLAB.

Â Am I just making an assignment, am I saying a sub i is equal to

Â zero so take the value zero and assign it to this variable a sub i.

Â Or, am I checking?

Â Am I, am I making an assignment or am I

Â like checking to see whether or not something is true.

Â And in the case where you want to check to see whether

Â or not something is true you use a double equals sign.

Â So we could have typed in something like this, if a sub i equals equals

Â zero then what you want to display is is zero value found in a particular index.

Â 20:26

Semicolons we've also seen used for vertical concatenation, right?

Â One, four, two, semicolon eight, zero, three, in

Â this case, will create a two by three array.

Â What have we used the colon for?

Â We've used the colon to create batches of equally spaced numbers, right?

Â If you said little a is zero, colon zero point zero one, colon five, it

Â will start at zero, to zero point zero one, go to zero point zero two, etc.

Â All the way up to five.

Â 20:52

We've seen examples of that.

Â We've also used a colon in order to

Â access all the elements along one dimension, right.

Â If I said, if we said little a is big A, three, comma, colon, this

Â will give us the third row of A and all the columns of big A.

Â The period is used to perform array computations versus matrix computations.

Â As we've discussed, saying big C equals A times B

Â is different from saying big C is A dot times B.

Â The dot times in this case says

Â perform this multiplication on an element-by-element basis.

Â And then we see an apostrophe used for a couple of times.

Â 21:31

For a couple of different things.

Â Apostrophes can be used to transpose a matrix.

Â If you say capital B equals capital A apostrophe, you can take this

Â tall and skinny matrix and convert it into a short and fat matrix.

Â And then we've also seen apostrophes used to delineate string variables.

Â If you said little a equals apostrophe I love matlab, apostrophe, then

Â little a in this case is not going to be equal to a number.

Â It's going to be equal to this text, I love matlab.

Â 22:12

We encountered this, function called find.

Â This is a very useful command for determining not whether something is

Â true, but, not whether something is true but where something is true.

Â So maybe what you care about is not

Â whether you have a particular value in your array

Â that is greater than a particular number but

Â which indices in your array are greater than that.

Â And that's where find can be very useful.

Â And then we also discuss how MATLAB includes several

Â convenient methods for writing out results and reading in results.

Â [NOISE]

Â We'll conclude this lecture with a self assessment question to

Â see how you understood the concepts that we just discussed.

Â 25:22

In this case you said, x at peak equals number to string index.

Â If index tells you the 16th value of y is the largest, you need

Â to type x parenthesis, index, end parenthesis

Â to display the course finding value of x.

Â In this case is roughly 0.37.

Â So, index, in this case, just equals 16, because you can count up here, and

Â you can see when you get to the 16th one, that's where you're at a maximum.

Â But the number you really want is if you drop down a

Â line here, to say that you're at 0.37, and you're at the maximum.

Â And to display that, you have to take x parenthesis index end parenthesis.

Â