 So in this part of the lesson we're going to look at arrays. It's quite a long section I'm going to show you a lot of what arrays are all about and what you can do with arrays. Now in most instances we're going to perform some functions on arrays and that's exactly what we're going to do in this lesson I'm going to show you quite a few Julia functions and how they impact arrays just to showcase what an array is. Now first of all, here's our first array I'm going to call it ARR, call it whatever you want within the constraints of the syntax that is allowed in Julia. And you see I Enclose this in square brackets and I put commas in between the values. I have 10, 12, 14, 16 and 18 and when I output that to the screen lo and behold we have the five element array, the all integer 64 and you see this comma 1 comma 1 means we're talking about a column So if you use other Development interfaces such as development environments I should say such as a light table with a Juno add-in It'll actually be called a vector, a column vector because this is indeed just what it is It is a column. Now let's just do a row vector So I'm going to call this array underscore ARR at least underscore row and you see I've still got my square brackets But I've omitted the commas as the spaces in between and if I run this section of code You see the output there. It is going to be a one by five array One row five columns all 64 bit integer values and C comma 2 This is now I don't really want to call it dimensions because it gets confusing as far as math is concerned But as far as Julia at least is concerned this 2 refers to this as being this is a row and you see the one there Meaning that that's a column Now Let's just use a function to populate an array But I want to do so this way sell all output toggle. So we just get rid of what's to come Keep it as a surprise so Ray ARR underscore rend and I use this rend function and it says five give me five Values and they I have a five element array. They are 64 bit Floating numbers values and they are columns it's down a column and you see what renders it chooses values from zero to one Inclusive and there's a pseudo random selection of these values right there Let's create an array in a different way by using the full Function full takes two values here seven comma ten. I've called My array sevens and if I element execute that you see it's a ten element array of 64 bit integer values in a column format and they're all sevens So the argument was what to fill your array with and how many elements you want so ten element array there I Can also use it on strings. So yeah, I have a string I love Julia exclamation mark and I want three of those and Low and behold, this is a three element array of ASCII string type again. It's a column array Now, let's use this bang Equivalent to our full function. So it's full bang and we're going to take LVE which was our array there and this is what we're going to append to the end and now I will have If we just select that section run it. We see we have a three element array now Adding a bang to permanently change so that is still three. Let's run this again. I Have a three element array. I fill it and I add a fourth one and I still get three back So clearly you see here this full With the bang is not really changed this array and we'll get to that Now Julia can repeat more than just a single value. Yeah, we have rep matte function I have my array here one two three as a column vector and I wanted to repeat it three times Let's run that There we go. We see a nine element array now of 64 bit integers in the column one two three one two three one two three Now using line space or lin space and log space So it goes like this Lin space start stop and how many elements you want you're gonna break this up the values one to twenty one in one to ten and twenty one equal bits and Well, I shouldn't say bits. I mean little Ranges type of you see is a linspace of 64 bit values So that's a bit different from just being an array now Let's print each of those so for iron lins print line I end print line means it's gonna all go in its own line And there you see it starts at one it ends at ten and they're twenty one of these little elements So it's taken it's gone between one and ten and it seems to have added 0.45 to each step log space takes these two arguments two and four and it says divide Divide up some values Between ten to the power two and ten to the power four now It's going to do by default. It is going to do it is going to do 50 50 arrays 50 elements there so Ten to the power two is two hundred ten to the power force ten thousand day and it's got 50 equal Well, there's a equal difference between each of these elements and there are 50 of them in total Let's do a few other things we're gonna create two arrays of random values Remember and it's gonna choose between zero and one inclusive and I want 20 of them for both of these computer variables Vals one and vals two I'm using semicolon just to suppress the output to the screen Well in this case doesn't really work In I Julia here It's still going to output One of the two arrays for me at least doesn't matter maximum bells one Let's run let's run that I want the maximum of all of those now every time you run this you're gonna get something different But it seems that value open nine five eight six, etc. Was the largest of all the values Of all the values in in in one so you can clearly see this was vals to that was what was printed to the scheme because vals one Sydney or vals to Sydney doesn't have this number in it Vals to here as displayed. I can say find max that returns the maximum value and its index So it says in vals one this number 0.95 86 one, etc. Was the largest value and it was in position number one Same goes for minimum. That's just going to return the minimum value and Find men will return that minimum value and its position extrema Go through an array and it's going to give us the minimum and the maximum values in that array Now just some descriptive statistics quite easy as well You can I just ask for the mean of vals one. It'll give you that open five five one eight five, etc Remember the equation for the average or the mean is just summing all the values and dividing by how many values They are how many elements they are and to count the elements in an array. We just use the length function So some all the values divided by how many they are and it's just going to give us back exactly the same as what mean did We can also ask for the median and we can also ask for the sample standard deviation Now I thought let's just include Let's just include a bit of fun Remember the equation for the standard deviation sample standard deviation the population standard deviation just divides by n But the sample standard deviation device by n minus 1 so you take the average of all the values and You're going to subtract from it from it each individual value and square that and Divide it by n over 1 and it's the sum of all of those The sum of all of those now. Let's just create that in a little function So I'm going to create a function. So that's just for fun I'm going to call my function sample standard deviation and it's going to take an argument x I'm going to implicitly have these little Computer variables. Yeah, I'm going to have total and I'm sitting that equal to zero So this is the local variable with respect to this function won't exist outside of the function N is the length of whatever array I pass to it So that I know how many elements they are and I'm defining this function average and The average is going to sum all the elements in the array that I'm going to pass this function divided by n, which is the length Now I'm going to run through a little loop for for I in 1 to n remember n is The length of the elements so it's going to run through all of the elements It's going to total which is zero when we start it's going to update the total with the average minus whatever Element I am at the moment and square that so that's going to be this numerator bit here and Run through all of those so that I have a total a new total now total existed outside of this for loop So it is explicit as fast as for loop is concerned But it's still implicit is still a local variable as far as the function is concerned But I can return summing the square root of the total which is now Updated all these differences between each element in the average and divide by n minus one and take the square root of that So there's my function. It's a genetic function with a single method because I Which is any because I really didn't specify anything as far as Limiting what x should be now? Let's use our functions then sample standard deviation and I pass it the val's one and it returns 0.314 what was the standard deviation 0.314? Let's just make doubly sure I'm going to say is Julius standard deviation Equal to my sample standard deviation function and of course that's going to return a true Now we've seen maximum and find max and minimum and find min. I just want to show you what max and min does max and min will look through More than one array and it will go element by element and it's got to be of equal length these element I have so that they've got to have these pairs and it'll just return the maximum of the two and Same for minimum. It's going to return the minimum of the two Now we can perform element wise operation So I can ask this Boolean question for val's one and val's two dot smaller than so it's going to compare The first element in val's one and the first element in val's two and see if it is smaller and then run through all of them And it'll return this Boolean it'll return this Boolean Values here so false if it's not smaller than and true if it is smaller than Now I can perform this little Change the value, so I'm going to say in val's one and Then then these square brackets if val's one is less than val's two set that value in val's one equal to a hundred I'm just using a large number now if I ask is Everyone in val's two less than val's two and they will all be false because the ones that did return a true We're now suddenly changed to a hundred Almost getting to the end of this section of the lesson. Let's do a few more things Remember the collect it's going to start at one increment in values of two and end at 21 I'm going to add that to odd and you see odd is now OD. I shouldn't say just OD I called it as an 11 elementary One there. It's a column vector one to 21 Now I can use the find function and that's just going to return the index value So find is odd in that So which of those are odd values and it says one two three four five six seven eight nine ten And all of them all of them were odd numbers and they were indeed all odd numbers So the find function here is going to return just the index value for me Return all the prime values for me in that and it gives me that but it's the index of that So if you wanted the actual values you have to do this give the variable name then find is prime in odd Because in this square bracket, it's just going to return an index So it's going to show all the values in this OD that are actually the prime numbers and you see them there Remember one is not a prime number We can just use this normal start stop instead of collect So I'll call it nums, but just remember this is a unit range. It is not an array if I use collect It's with start step and stop I can create an array, but if I just use this Range notation here 80 to 100. It's going to have the values 80 81 82 But as a unit range now, I can still iterate through it because I can just say any is prime numbers and It says true. So the any is just going to return a true is just asking the question Are there any prime values between 80 and 100? Yes Find the prime numbers between 80 and 100 For and now you say 4 10 and 18 you see there So once again, you can see what it returned a return the indices for me So if I wanted to return the actual values, I've got to have the indices since these are the indices I Specify them for nums and that returns for me the three elementary 83 a 9 and 97 are prime numbers filter It's going to do the exact same thing for me, but just give me the values all in one go The all function on an array all is in integer nums. Yes, indeed. They all are so they've all got to be true For this to return true. So if I ask are they all even it's just going to return a false No, they are not Now just for a bit of fun. I'm going to Go to the type max of my 32 bit integer values I'm going to subtract 10 million from them and then have this unit range from that till type max I'm going to have this 10 million values and So you see there from that value to that value and I'm going to use this macro at time to time Finding all the prime numbers Going through this list of real 10 million very large values Now the first time I run this it's a bit of a cheat because Julia's got a compile and everything not just executed. So the first time that you run this it's going to be very slow the second time the code has been compiled optimized for the For my CPU in this computer and it'll show the proper timings of all of this and The first time as I say this one is going to be very slow Remember I've got 10 million very very large values for it to go through and there you go You see it found six four hundred and sixty five thousand eight hundred and seventeen elements. So there's prime values in of those last and those last Values and you see the runs took how long they took and you see the open seven 72% GC time that it took so you can play but with that run that code again You'll see you'll see what happens see the large memory allocation that was required to do that So let's just have a small little array one two three four five and Very last thing in this I just want to show you combinations and permutations as it can be used on arrays so collect for me all the combinations in CH and Make three so take three of any of these and see how many combinations we can come up with Let's run that and it says you can have one two three one four one five one three four one three five one four five two Etc. You'll see it's a ten element array Permutations works a little bit different You can't choose how many of the elements you want to choose It's just going to go through all that one two three four five and come up with all the different permutations And if you know the equation for permutations, you'll you'll you'll you'll can calculate that there are exactly 120 permutations So quite a long section of this lesson This is going to be a very long lesson because I'm introducing both collections and Watch just giving you a showing of what can be done with him next up. We're going to modify our arrays