[BLANK_AUDIO] Hello. Welcome back. Now we're going to move on to part two of our series of lectures on Computing with MATLAB. 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. 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. 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. 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. 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. 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. First step, of course, is to click on the MATLAB icon and start the program. 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 script editor. And we can see that they're all here. Plot LR version one, version two, version three, version four. All four of these scripts will generate roughly the same sort of output, but they're ordered from I guess what I would call the, the stupidest one to what, to the one I would say is the most sophisticated. 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. As we go on from one, to two, to three, to four we're going to go about this in a, in a smarter and more systematic way. Okay, so the first thing is that when we're over here in the MATLAB editor there's several ways we can execute all these MATLAB commands. One is we can just take em, and we can copy the text, and then we can go over here, and we can paste the text. And we'll see that five windows pop up. These are the five plots that we, we said we wanted to generate. This is ligand receptor complex versus ligand for Kd equals. Here it is for Kd equals 30. For Kd equals 50. 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. 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. 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. 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. That will close all your plot windows. Lets take a look at plot LR version one right now, so we can interpret what it did. How did it generate these plots? The first thing to note is that, that the commands I put in here are one. Ligand equals 0:0.01 to 200. If we go back to our first lecture, we remember how the colon is used in this case it will, this will generate evenly spaced points starting at zero and ending at 200. 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. What about our numerator? Our numerator is also going to have 20,001 elements. Ligand has 20,001. And R tot is a scalar. So you're going to have many elements in the numerator and many elements in the denominator. But what we want when we, when we calculator ligand receptor complex is, the given value of ligand divided by the corresponding value in the denominator of, of ligand plus Kd. That's why we use the dot divide rather than the regular divide. 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. Typing clear before you run a script is good practice. Because that, you don't want, you want a script to be able to stand on its own. You want it to be able to work, because you have correctly defined all the variables within the script. You don't want to create a script that only works because you happen to have some variable that was already defined previously. 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. Let's close all of those. So like I said, this script kind of works. But it's certainly not ideal. 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. Now let's think about how we can address that. What we want to do now is we want to move to version two. Like I said, these are ordered from kind of the, the dumbest one is version one to perhaps a smarter one as we go to version two, version three, version four. 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. 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. 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, C is for cyan. Let me show you an example of how this works. We're going to type a new figure here, and now we can plot L comma LR. The default color is going to be blue. What if we want to make it red? You can type plot L versus LR, and now make this with an R. This will plot the same thing with red. This will plot it in green. 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. You'll see that's what it is. It's these six letters repeated over, and over, and over again. And 300 in this case is arbitrary because I can't imagine that we'll ever want to plot more than 1,800 6 times 300, or 1,800 graphs on one on one plot. You notice I just said we were going to plot them on one graph. That's key to what we have here that's a little different, right? That was dif, When we did version one of the script, we had five different graphs, and that wasn't very helpful. Now we have five graphs all in one plot, and that makes this a lot more helpful. The key thing I did here is I take this, hold on. 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. 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. You'll also notice here I've defined an x label, a y label, and a legend. 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. 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. And then we when, if we wanted to plot it you know, the 300th element of colors, or the 400th element of colors, we'd have to keep adjusting this number. You can see how that would become, that would become very tedious, very quickly. So that's what we're going to want to to fix when we move to plot LR version three. So let's go back to our Command window. Let's close our, all of our figures. Let's clear all of our variables. I'll press the up arrow to scroll, scroll through the previous commands. Now we want to plot LR version three. We see the output looks the same, so clearly we're calculating this correctly. It looks exactly the same as it did with version two. But now if we look at the code, we look at the MATLAB script version three, we'll see how this one was, was a little bit smarter. Let's close version two. And now let's take a look at version three. What do we see that, first thing we notice with version three is that the code is a lot shorter. It doesn't take up nearly as many lines. 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. 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. For i equals 1:5. And then it ends with a value called anin. And then we have all these commands in-between at the beginning of this for loop, and at the end of this for loop. 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. A for loop is way to, to do that as many times as you want. 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. Ever, the first time you go through Kd will be equal to the first value of Kds, the second time it will be equal to the second value etc. That's because we define this Kd equals Kds sub i. 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. So this is our way to make this very flexible so that we don't have to keep copying and pasting these lines where we calculate ligand receptor complex, and we plot it each time. The other thing I've done down here is defined a figure legend. 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. The third time we want to add this. There's couple things that are, that are really important here one is that, the, the text that goes on, into it is defined by this. 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. 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. 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. 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. 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. 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. That's because they don't have compatible dimensions. But what if you want, if you need something that's going to store a in and be together? Accelerate can be useful for this. You can say c, sub one. And remember, these are the curly brackets. Equals a. And then you can say c sub 2 equals b. 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. So I took, so this MATLAB is okay with this. You can have, you can combine numbers you can combine arrays and you can combine text and and cell arrays. The reason I defined figure legend as the cell array is because we don't know that each time we go through it's going to have exactly the same number of characters in it, right? If our Kds went up above 100 then we would have an extra character, and MATLAB might have a problem with that. So if we define it as a cell array it's no problem. 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. 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. We'll type close all. Now what I want to do is plot LR version four. 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. The more important thing here is, we defined this new variable called LR_all. Remember that one of the problems with what we had before, with version three, was that every time we moved down to a new value of the Kd and we calculated a new set of values for the ligand receptor complex, the old one was gone. 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 all of our values stored. Now we're going to go back to the go back to the PowerPoint. And we're going to sort of review some of the things that we, we, we learned through this exercise. And we're going to talk more generally about for loops. Now that we've generated this plot, let's summarize some of the principles of the MATLAB programming environment. That this example has illustrated for us. 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. Another really important principle that this illustrated is the idea of array arithmetic. Remember that when we calculated ligand receptor complex. 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. We also learned so of the principles of programming logic. We could either type all the commands into the command line. We could, type all the commands into any editor, and then copy and paste them into the command line. Or we could save all the commands as a script and then type the name of the script at the command line. 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. 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. Some notes or some hints on using for loops. Remember that all commands between the word for and the word end are going to get repeated each time you go through the loop. 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. 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. 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. If you want to repeat calculations, which you often do, you often want to do something multiple times. For loops are the most convenient way to set up that sort of structure. And if you want each time through the for loop to be slightly different what you should do is you should perform calculations using the index variable. And the index variable is what's going to allow each time through the for loop to be slightly different from the last time through the for loop. So as usual, we'll conclude with a self assessment question. 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. So what you want to write is a for loop that plots four time courses in different colors all in the same plot. You paste the following lines in your Command window. Colors equals KRGB. We discussed in our example what that means. 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. 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. 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. So specifying A colon, comma i, tells MATLAB to plot a different column each time through the loop. Specifying color sub i tells MATLAB to plot a different column in each color. So that concludes our second lecture. Thank you and next time we will be discussing some more even more sophisticated approaches that can be used in MATLAB.