Get startedGet started for free

Operating on arrays

1. Operating on arrays

Arrays are an efficient way to process a lot of data.

2. Basic array functions

One basic piece of information we might want from an array is to know how long it is. For this, we can use the length function on the array. The length function returns an integer of the number of elements inside an array.

3. Basic array functions

Another helpful function is the sort function. This function sorts the elements in an array by their values. For string data like this, it sorts them alphabetically. For numeric data, it sorts them from lowest to highest.

4. Vectorized operations

Let's go back to our run times. Here are the times plotted against the run number. Let's say we have fit a line to this data and already know that the gradient is negative zero-point-three-two and the intercept is thirty-four-point-eight. We want to predict what our next run time would be if this trend were to continue. To do this, we multiply the new run number by the gradient and add the intercept. Then we can print our time target. But what if we want to make predictions for our next five runs?

5. Vectorized operations

We could change the run number and run this script multiple times, or we could process the predictions with arrays. Our next five run numbers are in the array x. But if we try to run our previous code to predict the times it will cause an error. Julia cannot process arrays like this. Instead we need to use vectorized operators.

6. Array addition

Say we have an array a, and we want to add a scalar to each of its elements. In Julia, we can do this using the dot-plus operator. Putting a dot before any operator means that the operation will be applied to each element of the array. Two will be added to each of the three values in this case. We can also use the dot-plus operator if we want to add two arrays. With two arrays, the elements at matching index locations are added.

7. Array addition

We cannot use the regular plus operator to add a scalar to an array, although, we can use the regular plus operator to add two arrays. This is one of Julia's quirks.

8. Array subtraction

Subtracting from an array works the same as addition. We use the dot syntax to subtract a scalar from the array. We can use either the dot-minus or the minus operator to subtract two arrays.

9. Array multiplication

We can multiply an array by a scalar or another array using the dot-star operator. When we multiply by a scalar, each array element is multiplied by this value. If multiplying by an array, then the elements at matching indexes are multiplied.

10. Array multiplication

We don't need to use the dot syntax to multiply by a scalar, but we need to use it to multiply by an array.

11. Array division

Dividing is similar to multiplying. We can divide an array by a scalar using dot-slash or slash. We need to use the dot-slash operator to divide the elements in an array by the elements in another array. The slash operator won't raise an error, but it performs a different operation which we won't cover here.

12. Vectorized operations

Returning to our run time predictions, we need to replace the operators with their dotted versions.

13. Vectorized operations

We could have used the star operator instead of the dot-star operator, and the results would have been the same. However, using the dot syntax is more explicit.

14. Cheatsheet

Here is a cheatsheet you can refer back to in the exercises.

15. Let's practice!

Let's practice.

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.