0:17

So today we're going to talk about using

MATLAB for data manipulation and data analysis,

and the core of today's lecture is

going to be an example of MATLAB array arithmetic.

So I talked a little bit in the first lecture about differences

between vectors and arrays, and maybe a lot of that seemed really abstract.

Today we're going to show you an example of

how we can use MATLAB array arithmetic, and hopefully

some of these abstract concepts will become more

concrete when we use MATLAB to actually do something.

And with this example it's going to show us is, how we can plot results.

I's going to show us some prin, some general principles of programming logic.

We're going to learn in this example how to manipulate

arrays and we're going to also learn this concept called for loop.

Once we finish this, I'm going to, we're going to discuss in

a little bit more detail some strategies for using for loops.

1:05

So the example that we're going to show is we're

going to try to find a, a biologically relevant example.

And what we're going to plot is with the is bound receptor as a function of ligand.

So the idea here is that you have a receptor, which

we call R, that can bind to a ligand called L.

So when the ligand and the receptor

come together you form a ligand receptor complex.

These two come together with an association constant called

K plus, and then they come apart with the disassociation constant called K minus.

Now this is, this problem has been well studied in a lot of biology and you

know, through a lot of through a lot

of biochemistry, and people have analyzed it mathematically.

And they've determined that in steady-state, you can

calculate the bound receptor using the following formula.

LR is equal to the R tot times the ligand times KD over the ligand.

Where R tot in this case refers to the total number

of receptors that you have, and this constant, KD refers to.

Is, is defined as the, the off-rate K minus divided by the on-rate K plus.

2:12

So, what we want to do is, we just want to plot this equation here.

We want to plot ligand receptor complex versus

ligand level for a range of different Kds.

So what we're going to do now is we're going to move out of Powerpoint.

We're going to move into the MATLAB programming environment.

And I'm going to show you how you can plot ligand

receptor complex versus ligand for a range of different Kds.

Different ways that we can do this and it's going to illustrate the [INAUDIBLE].

This slide here shows what we're going to

get after we've produced these plots using MATLAB.

Again, the equation we're plotting is ligand receptor complex here.

As a function of, of free ligand, and also a function of the parameters Rtot,

the total amount of receptors, and Kd, which

is the association constant for ligand receptor binding.

We're going to plot this equation for Rtot equals 20 nanamolar.

Ligand is going to vary between zero and two hundred micromolar.

And then we're going to plott it for

five different values of association constant or the Kd.

Ten, 30, 50, 70, and 90 micromolar.

3:18

This plot down here shows what we're going to get.

Ligand receptor complex on the Y-axis.

Ligand on the X-axis.

And you can see five plots, of course

binding through the five different values of a Kd.

Those of you who are familiar with biochemistry

and cell biology, probably already know that the

black curve here that rises the most quickly,

corresponds to the lowest value of the Kd.

And the magenta curve down here.

Which rises the most slowly corresponds to the highest value of the Kd.

With the others in between.

3:50

So what we want to do now is

we want to move to the MATLAB programming environment.

And again, we want to exploit MATLAB's functionality to generate this plot.

And, in doing so, this is going to illustrate some

of the principles of MATLAB programming that we've been discussing.

[BLANK_AUDIO]

What we're going to do now is we're going to

open the MATLAB programming environment, and we're going to demonstrate

how you can, I can use the MATLAB programming

environment to perform some simple calculations, and generates some plots.

So there's two things you can notice here on the desktop of this computer.

One is that the, there's a little MATLAB icon down here.

We've successfully installed program.

And the second we have a folder here on the

desktop, called MATLAB code that has four files in it.

4:36

These are called Plot LR.

LR for lygand receptor complex, remember that's what we're

going to plot, and then version one, two, three and four.

So now I'll demonstrate how we can, how we can use

MATLAB programming environment to generate the plots that we just showed.

4:57

And there we are.

Now, these files we have in here, Plot LR version one,

two, three, and four, these are what are called MATLAB scripts.

So we want to open these up, and be able to look at them using MATLAB's editor.

So we can go to Open, and, or sorry we can go

to New, New Script, and you'll see that this editor pops up.

This is the MATLAB, script editor that you

will use to, to modify your scripts in MATLAB.

And now we can take these four files that we have stored on the,

on this folder that's in the desktop, and we can drag them into the

5:56

And, and the cleanest one.

And in going through these step by

step, we're going to illustrate some, some principles

that are useful and illustrate some strategies

for for using MATLAB for scientific computations.

So if you, if you're familiar with programming and you see the first

couple of ones and you say, why did he do it that way?

He shouldn't have done it that way.

That's, that's correct.

That's the whole idea.

7:05

For KD equals 70, and for KD equals 90.

So we did that, we were able to generate these five just by copying and pasting the

text from the MATLAB editor into the, what this,

which, which known as the command line down here.

That's not always the best way to do it.

A better way to do it, is to when you have a, a

MATLAB script that you want to run, is to press the Run button here.

And you'll see something that pops up.

This will say, we're not in this, in the correct folder.

This particular folder that you are in is not part of the path.

Then what you want to do is you want to change folder.

7:41

And if you look back here on the MATLAB command line,

you'll see now we're in the

in the correct folder: Users\Coursera\Desktop\MATLAB Code.

And if we're down here at the command window,

we can type ls, which is the command to list

all the files that are in, in the current

folder, and you'll see that our four scripts are here.

Plot_lr_v1.m, version two, version three and version four.

All dot m, the dot m extension in this

case tells you that's, that it's a MATLAB file.

8:11

And you can see that it popped up the five windows.

Version one, I mean sorry, for Kd equals 90, 70 30, 10.

Now one thing you noticed is that you know,

each time I ran this, whether I copied and

pasted it or whether I, I ran it by

pressing by pressing Run, it popped up five windows.

Let's do something else.

Here's a, here's yet another way to run it.

Plot LR version 1.

You can just type that at the command line.

Because plot LR version one is the name of the script.

The way MATLAB interprets this is if you typed the name of the script,

in the, the command line, it will execute all the commands there in that script.

8:53

So if you type plot LR version one, again, it will pop up five windows.

You notice that every time we run this, it pops up five windows.

And, I've been trying to close them in between, but if I hadn't

closed them, we would suddenly have 15 plot windows up on the screen.

And if you're doing do with this, if you're

actively computing using MATLAB and, and putting up plots

to generate things, then it's easy to get lost

in, in how many different windows you have up.

So a helpful command to use is, is Close All.

9:54

We define the total amount of receptor equals 20.

Then we define the Kd.

The Kd is equal to 10.

Now, this line here is really key.

Ligand receptor equals receptor total, multiplied by ligand.

And then we have a dot divide.

And then in the denominator, we have ligand plus Kd.

Let's recall what the dot divide means.

In the denominator, we have ligand, which has many, many elements in it.

In this case, it's in fact 20,001 elements plus Kd.

Kd is just the scale.

Kd is a number, but it's okay to add a number to to a vector, or to an array.

It will just add Kd to every value in, in ligand.

But our denominator in this case is going to have 20,001 elements.

11:05

Then we say, make a figure, plot ligand comma ligand receptor comma LR.

This is saying, plot ligand on the x axis.

Ligand receptor complex in the y axis, that's what we want.

We put a title in here, Kd equals ten micromolar.

This little underscore here says when we put up

Kd equals ten micromolar, make it a, a subscript.

And if you looked at the, if you look at the titles on our

plots that's a sub Kd is the d is sub-scripted as it should be.

Then we say set Kd equals 30.

Perform the calculation again.

Plot ligand receptor versus ligand receptor complex in a new figure.

Do it again for 50, for 70, and for 90.

Okay, so this script, it kind of works.

Let's run it one more time.

[SOUND] Okay.

One more, one more piece of piece of programming advice.

It's also good to type clear before you, before you run your scripts.

That will clear all of your variables.

In fact, if you look over here on the work-space, you can see.

These are the variables that are currently

defined; Kd, ligand ligand receptor complex, and

our tot, and when we type clear, all these variables are going to go away.

12:39

And, and that can happen if you don't clear it before you run the script.

Maybe it'll run correctly, because you, you

did define some variable but when you try

to run it later when you haven't defined that variable, it, you'll get an error.

So, it's always good to type clear before you before you run your

script just to make sure that the script can run on its own.

So now what I'm doing is I'm pressing the up arrow.

You can see that as I press the up arrow

it will scroll through the commands that I previously used.

You can also see that the commands I previously used are over here.

So if I'm scrolling up I can see plot LR version one, I press Return.

And it'll generate these five plots.

For Kd equals 90, for Kd equals 70, 50, 30 and 10, et cetera.

13:33

There's a couple of issues with this this particular script as it's run.

One is that we have five different plots for different

values of Kd and they're all, they're all in different plots.

We want to be able to compare how does the curve look for

Kd equals ten to how does a curve look for Kd equals 90.

And it, right now it doesn't allow us to do that.

So that's one problem with the way we did it.

And another problem is that we don't have labels on these plots.

We have a title.

Saying what the Kd is, but we don't actually have labels saying this

is what's on the x axis, this is what's on the y axis.

14:25

So now again we, we, let's we close, I think that

none of our windows are open but we'll close, we'll press Clear.

Now what we want to do instead of version two, we'll type Backspace, and

then I'll, instead of version one, I'll type Backspace and I'll type two.

Let's do plot LR version two.

14:41

Now that's better, right?

Now we have all five of them in different colors, all on the same graph.

And we have a, a label of our x axis.

This is ligand in units of micromolar.

And we have a label on our y axis.

This is ligand receptor in units of nanomolar.

Let's take a look at script version two to see how we did this.

15:02

Here's version two.

First command I have in here is I say colors

equals this MATLAB function of repmat, which means repeat matrix.

And now I have krgbmc, one col, colon.

One comma 300.

I, I set this up because sometimes if you have many plots, you

want to have them in a, in different colors in a particular order.

And this is how MATLAB interprets different colors.

K is for black, r is for red, g is for green, b is for magenta,

16:10

So what I've set up here with LR version two is.

First time we plot one, I want make it, I want to do it black.

Second time I want it to be red.

The third time I want it to be green, et cetera.

And if you end up with more than six you want it to repeat.

You want the seventh one to be black, the eighth one to be red, et cetera.

What this repmat will do is will take this

string, KRGBMC and it will repeat it 300 times.

So if we go to the Command window, and we type colors.

17:19

So in the previous version, in version one, every time

we did this calculation, I said make a new figure.

And that's why they ended up on new figures.

I, I had this figure command.

In case you only want to do the figure

command once, but we also have to type hold on.

That tells MATLAB I'm going to plot more I'm going to do more and

I want you to plot it on top of what's already there.

17:41

So now as we go through for Kd equals ten, 30, 50, 70,

90, you see that they all, they all show up on the same plot.

That's because we typed hold on, and also

because we're not opening a new figure here.

The other thing you'll notice is I said plot ligand comma ligand receptor, and

I said first time color sub one, sub two, sub three, sub four, sub five.

That's how they end up in the five different colors.

Black, red, green, blue, and magenta.

18:13

So if we go to our figure, the x label over here tells us this label, right.

The y label here tells us this label, and the

legend tells us to put a a legend over here.

One more thing that you'll notice here that we haven't

talked about in the examples we've shown in the lectures.

Is this, comma dot dot dot.

18:35

What if you have a really long command?

And you want to, and, it's too long to put it all on one line?

It's going to stretch way, way across the screen.

And you're not going to be able to see it on your screen.

You can move you can continue the command on the next line with this dot dot dot.

So the way MATLAB interprets that is that okay whatever comes next on

the next line is part of the is part of the same command.

So version two is clearly a lot better than version one, right?

It's better because we can plot all five of them on the same graph.

But there's something that you may have noticed about version two

that makes it still inadequate for what we want to do.

Which is that, I keep typing, if this is the value of Kd and

then this is the next value, this is the next value and the next value.

And then I have to copy and paste.

Calculate ligand receptor complex again, plot it again.

Every time we want to do this we have to copy and paste these lines.

What if we wanted to not do five values on this.

But what if we wanted to do 500.

That would get pretty tedious right.

We would have to keep copying and pasting it every single time.

20:52

This looks, this is the same.

We define color as we define R tot.

We design, define ligand the same way.

Notice a couple things that are different.

One is that, instead of just a variable called Kd

that's a scalar, Kd equals ten or Kd equals 50, etc.

Now we define a new variable called Kds with an s

at the end of it, and how do we define this, 10:20:90.

This says start at the number ten.

21:20

Have an increment of 20, and end at the number 90.

So Kds will be equal to ten, 30, 50, 70, 90.

In fact, we can go back to the Command window, and we can type Kds.

And we can see.

These are the values.

Ten, 30, 50, 70, 90.

So now we have a variable called Kds which holds all the different values of Kd.

Instead of defining each one by itself.

We also type figure and hold on the same way that we did before.

But the main thing that's different is this

element down here which is called the for loop.

22:09

We are going to talk about for loops at some

length, because this is a very important and powerful programming tool.

For loops are, are used when you want to repeat some

certain calculation, repeat some command over and over and over again.

Like I said before when we were discussing version two, it

would get really tedious if you wanted to plot 500 of them.

Because you would have to keep copying and pasting

the same code 500 times and adjusting it each time.

22:41

So the, the way to, define a, a for loop is like this.

You say for some variable equals some starting number up to some ending number.

And it's often used with a, with a colon like this because.

What, what the way MATLAB interprets this is start

at the value one, end at the value five.

And each time we go through the for loop,

there's going to be this variable, i, which is called

the index variable of the for loop, which is

going to change each time you go through the for loop.

So it'll be one, two, three, four, five.

Now MATLAB has helpfully highlighted i so we can see how i is used in the for loop.

23:31

This is our way to change Kd each time we go through.

Ten, 30, 50, 70, 90 defined by the five values in this variable Kds.

This is also a way to plot the five different colors.

Before we said color, we manually said plot it

in color sub one plot at color sub two etc.

This way it will cycle through and the first time it will plot it and the first

element of colors the second time it will

plot it in the second element of colors etc.

24:17

That is going to be the, the, it's going to hold all of

our values for Kd equals ten, Kd equals 30, Kd equals 50, 70, 90.

So this figure legend here is, is being defined as we go through the for loop.

Remember, you know, the first time through we, we go through the

for loop we want to, put this text in our figure legend.

Kd equals ten micromolar, the second time through we want to add this.

24:54

This is defined with a, closed bracket at the beginning.

And a closed bracket at the end, the same

way that we would define a vector of numbers.

But in this case, it's defining a vector

of, of characters, it's defining a string vector.

But it's very similar to what we would do if we were defining a vector of

numbers where you'd have a closed bracket at

the beginning and a closed bracket at the end.

25:15

The first text we want to have is Kd equals.

And the last text we want to have is, is micromolar to indicate the units,

but in between we want it to be ten, or 30, or 50, or 70, 90.

But, we might want MATLAB to interpret this just as text, right?

We don't want MATLAB to interpret ten as equal to the value ten.

So, in the middle we've put in this command here.

Int 2, at int to str.

The way MATLAB interprets this is integer to string.

25:42

So this is taking a number, which in, in this case

is the variable Kd, converting it into a string so that

MATLAB will interpret this whole thing is a bunch of text

to put up on the screen as part of our figure legend.

The other thing we've done is we've said is the first time

through we want figure legend to be equal to Kd equals ten micromolar.

The second time through we want it to be 30, 50, 70, 90, etc.

So, we've set figure legend sub i is equal to this text.

26:12

By doing this sub i that's our way of of making sure that

the first one will be ten, the second one will be 30, etc.

There's one more little twist here that we're not going to go into in depth

in this class but this is something MATLAB

this is where MATLAB can be pretty powerful.

You'll notice that this is not a parenthesis, this is a curly bracket.

26:31

And this is a end curly bracket rather than an end parenthesis.

When we want to access the first element of Kds,

or the second element of Kds, we just use a parenthesis.

Here we're using a curly bracket.

This is a special type of object in MATLAB known as a cell array.

And, like I said, we are not going to discuss this in depth in the class.

This is something that if you want to learn about

it, you'll have to read up on, on, on yourself.

But cell arrays can be helpful when you don't when each

element of the cell array is either of a different type.

Or each element of the cell, cell array has a a different has a different size.

Actually, let me show you an example of a cell array just

so you can get an idea about how these can be powerful.

27:14

We, in the previous lecture, we talked about

how you can have a problem with concatenation, right?

If you say little a equals 1 to 3,

and you say little b equals 5, 3,

2 and then a ; sorry, 5, 3, 2,

8 and then a ;, 2, 1, 9, 23.

[SOUND].

Forgot to put the equals in there.

Now if a is one by three and b is two by four, you can't concatenate these, right?

C equals a comma b.

You get an error for using horzcat and similarly, if

you try to vertically concatenate them, you'll get an error.

28:34

Now if we look at c.

The first element of c will be a one by three double.

That's the numbers in a.

And the second element of c will be a two by four doubled.

So cell arrays are a structure that you can use.

If you're not, if each element is not going to have the, the same dimensions, or

if you could have, you could combine things

that are different different types using cell arrays.

For instance you could also say, c sub 3 equals string variable.

29:40

So if we look at version three we can see

how this is, this is far superior to to version two.

Right?

We, this for loop is, is very powerful.

This really helps our case in terms of not having to

type the same text in over and over and over again.

But there's a couple, a couple of issues with version three.

One is that I define this for loop here for i equals 1 to 5.

What if we wanted to add more elements to Kd?

Well, we would have to, we'd have to change this number five.

Or what if we wanted to have fewer elements of Kd?

Well, we'd run through it and get and error

because you would have that many elements in, in Kds.

There's a second problem with version three that is You know may

or may not be a problem depending on on what you need but.

What if we wanted to compare let's say you know how much ligand

receptor complex do I have at a 100 micro molar of ligand.

And I wanted to compare what's my value if Kd equals

10 to what's my to the value of Kd equals 90.

Well if we look at our variables in MATLAB,

ligand receptor complex is a one by 20,001 double.

Ligand is also a one by 20,001 double.

So every time we run through the loop, every

time we go to a different value of i.

We calculated new value of ligand receptor complex, but

what happened to the old value of ligand receptor complex?

The old value of ligand receptor complex is gone.

It's gone forever.

So first we calculated for, for ten micromolar.

We plot that, but then when we move on and we calculate it

for 30 micromolar, whatever we have

for ligand receptor complex for ten micromolar.

That's all gone.

31:24

So every time we go through, we we calculate a new,

value of ligand receptor complex, and the old one has, has disappeared.

So if we wanted to, to after the fact, compare these.

Compare what's, you know, what's the ratio of the red one to

the green one at this point, we wouldn't be able to do it.

So now let's move on to plot LR version four,

so we can see how we can overcome that limitation.

[BLANK_AUDIO]

We'll do what we did before, we'll type clear.

32:05

We can see the output's different.

But if we look at the code, we'll see how how the code in this case is, is superior.

So now let's close version three, and look at version four.

Two things here that are, that are much different with two

things that are different in version four compared with version three.

One is how do we define our for loop.

Remember, with version three, we manually said for i equals 1 to

5, which is fine when Kds has exactly five elements in it,

but if you want to increase it to more than five, you're going to

have to go in and, and change that in your for loop.

Well, we defined Kds up here.

What we want is we want, we want to run this

for loop for as many values as we have of Kd.

The number of values we have of Kd is defined, is determined by its length.

So if we set our, start our for loop at one

and end our for loop at length of Kds, that's going

to be a way to adjust as we add more elements

in Kds or subtract more elements to get this to work.

33:25

So, what we want to do now is keep all of them.

And these are all stored in this value called LR_all.

We say LR_all i comma colon equals ligand receptor complex.

That's going to say the first time through when i is equal to

one, we want this to define the first row of ligand receptor complex.

The second time through, we want it to define the second row, etc.

So now if we go back to our MATLAB Command

window and we look at how ligand receptor all is defined.

This is a five by 20,001 double, which

means that it's five rows, it has 20,001 columns.

And first row it will be all the values for the for Kd equals ten.

The second time through will be all the values for Kd equals 30 etc.

So this is a way to keep keep track of everything.

So now if we wanted it to perform more

calculations on LR_all, we would have all of our

34:46

First of all, we learned some of the rules of, of plotting in MATLAB.

We learned how to plot in different colors.

We learned that when you type figure, a new figure pops up.

We learned that you type the command hold on if

you want to plot several different outputs on the same figure.

And we also learned how to add a legend.

Which is the little box over here that tells

us which plots correspond to which value of Kd.

And how to put on a label for the x axis, and a label for the y axis.

35:25

the, the equation is ligand times total receptor divided by ligand plus Kd.

But ligand in this case has many, many different values.

It corresponds to all the values from zero to 200 micro, micromolar.

So the numerator in this case, ligand times Rtot is is a vector.

And the denominator ligand plus KD in this case is also a vector.

And therefore, we had to perform an array arithmetic.

We had to a dot divide rather than a regular

divide because the computation we wanted to perform is many different

values of ligand receptor complex for the corresponding value of the,

of the numerator divided by the corresponding value of the denominator.

So that, this illustrates and I, An instance where

you need to use array arithmetic, rather than standard arithmetic.

36:28

We also learned a really important

principle of, of programming logic, and we're

going to talk a little bit more about this in the next couple of slides.

But for loops are something that you can use when you need to

perform several calc when you need

to perform a series of calculations repeatedly.

And then finally we learned this illustrated some of the ways

that you can manipulate arrays and you can access parts of arrays.

For instance, when we wanted to create an array called multi-dimensional array

called LR_all, that held all of

the different values for ligand receptor complex.

If we wanted to access one row of, of LR_all, we could type in

something like this to assign the current value of ligand receptor

complex to one row of the multidimensional array, LR_all.

So now, now let's talk a little bit more about for loops.

These are one of the most commonly used and powerful programming tactics.

And these are used to repeat certain calculations several times.

This, for loops are one of the things

that makes MATLAB so powerful, because you don't

have to keep telling it to do the same thing over and over and over again.

You don't have to keep clicking.

You can just tell it how many times you want it to, to do something.

37:35

So the generic structure of a for loop is like this.

You say for, some variable equals, some sequence of numbers.

Then within the for loop you have a bunch

of MATLAB commands, those are going to get repeated each time.

And then when the for loop is over, you

type end in order to indicate that it's over.

So for example you can say for i equals 1 to 5,

output equals 4 times i raised to the second power plus 13.

What this will do is it'll calculate and display five values

of the variable output according to the formula that you've entered here.

38:18

Further more each time you go through the loop the index variable will be different.

The index variable what I, what I mean by the index variable is the

variable that It's changed each time, each time you go through the for loop.

So, for this example here, the index variable is what we're calling i.

That's a very typical you can use whatever variable you want in a for loop but

i is a pretty typical value that, that

programmers often use when they're writing for loops.

And the fact that the index variable's

going to be different each time can be exploited,

and this is a way you can make each trip through the loop slightly different.

So, something that we'll do when we're actually implementing

a dynamical models is we might say for i

equals 1 to 5, there can be some parameter that can be defined as a function of i.

So maybe the first time through the loop it'll be equal to two, the second time

through the loop it'll be equal to 4, the third time it'll be equal to six, etc.

And then you can run the model with all these different values of this parameter.

So the fact that you have this variable called i which is different each time.

Is a way that you can make something else be different each time.

And that's a way you, you know you, it's very rare

that you want to repeat exactly the same thing a hundred times.

What'll happen is you might want to repeat something a

hundred times but each time through you analyze a different file.

Or you, you change something, and by the

fact that your index variable changes each time, that's

a way to make it a little bit different each time you go through the for loop.

Right.

So, parameter, in this case, will take on values two, four, six, eight, ten.

Here, we see a second example of how one can set up a for loop.

In the previous example we, we took, the for loop

just went from the number one to the number five.

So, our for loop in that case just said, for i equals 1 to 5.

But what if you want to have several different values, several different values

of your index variable in your for loop, but they're un, unequally spaced?

I mean, what if for instance, the values you wanted to test in some sort simulation

or in performing some calculation were one, three, 13, 22, and 300.

40:16

You can set those values in a vector.

In this case, I'm calling values to test.

So this is going to form a one by four array.

And then you can say four i equals 1.

And now you could say four here, or i equals 1 to 4.

But in that case it wouldn't be very flexible, because then if you wanted to

add more values to test, or remove values

to test, in the dimensions of this changed.

Then your for loop wouldn't automatically adjust.

But in this case you can say for i equals 1 up to the length of the values to test.

Now if you changed the dimensions of values to test your, your for loop

would automatically adjust, and then you could

say parameter equals values to test sub i.

So the first time through the loop, parameter would

be equal to one first element of this vector,

second time through the loop it would be equal

to 13 the second value of this factor, etc.

So this is the way you can set up a for loop.

41:09

But have your index variable have the

have whatever value whatever number you're performing cal,

calculations on can be different each time and

it doesn't necessarily have to be equally spaced.

[BLANK_AUDIO]

So in summary, MATLAB can be used for performing calculations.

It can be used for plotting.

It can be used for lots of different applications.

41:58

If you're working with an array A, that has dimensions 100 by four,

and you also have a vector time, that has dimensions 100 by one.

So each column in A, represents a

different variable that was measured in your experiment.

And each row of A represents a

corresponding time point in the vector times.

So in other words the first element of time represents

time point number one and then you have four values

of a, your first row of A represent the the

four variables that you're measuring at that particular moment in time.

42:42

And then you say, for i equals 1 to

4, plot time comma A, parenthesis i end parenthesis, end.

So, this is not going to produce the desired result.

So now I'll give you a few seconds to think

about why, and then we'll, then we'll discuss why it

doesn't give us the desired result, and how we would

fix this to plot what we want it to plot.

[SOUND].

So the answer.

There's two reasons it does not work.

One is that, the idea is that you want to plot the

first column, the second column, the third column, and the fourth column.

But in this example we gave here, we did not instruct MATLAB to

take the first column, the second column, the third column, and the fourth column.

We're just telling it to take one element.

If you say a, comma.

43:28

A parentheses, i end parentheses, this is saying take the first element

of a, the second element and the third element, and the fourth element.

So it's only instructed to plot a single element of a.

And then there's a second problem here.

It says that MATLAB has not been instructed to

plot in a different color each time through the loop.

It's going to plot the first element of a in blue,

and it's going to plot the second element of a in blue.

And then the third one and then the fourth one.

They're all going to be in blue, but we want to plot these in different colors.

So how can we modify this simple for loop to get it to do what we want it to do?

We see this example here.

The correct lines to paste in the Command window would be as follows.

44:03

Again, we define the colors, black, red, green, and blue.

And what we say for i equals 1 or 4, we say plot time.

A colon, comma, i.

That would be the first column of A, the first time through the loop.

The second column of A, the second time through the loop, etc.

And then we want to plot each one in colors sub i.