Get startedGet started for free

Multi-dimensional arrays

1. Multi-dimensional arrays

In this lesson we are going to expand on a data type that we are already familiar with - the array.

2. 1D array recap

Recall from the previous videos that a vector is another term for a one-dimensional array. Creating a quick vector, we see that it only has one dimension. It is very easy to read and access values, as we only need to use one index to get any value in this vector. A vector is great for basic storage, but as we move to larger and more complex data, we start to require more dimensions to adequately store our data.

3. 2D array structure

This is where the 2D array comes in. In Julia, a 2D array is also called a matrix. This is a tabular representation of data, and looks a lot like a structure we would see in the real world, in an Excel sheet or other data sources. To define a matrix, we remove the commas between elements, and instead use a semicolon to denote a new row. Let's take our vector from the previous slide and convert that into a matrix, with two rows instead of one. If we print out our matrix, we can see that it does indeed have two dimensions.

4. Indexing a 2D array

Now we have our matrix, how do we access the values within it? We use indexing, just as we did with a vector, but this time we need two indexes - the row and the column. Let's say we want to print the value two in our matrix, which we can visually see in the first row and the second column. We use the square bracket notation and write the row, one, and the column, two, and we get our return value of two!

5. Slicing a 2D array

We can slice matrices, just as we can with vectors and DataFrames. This is done using the colon operator, which allows us to return entire columns or rows. Let's try return the entire third column, which means we want every row in the third column. To do this, where we would normally write the index for the row, we write a colon. Printing the result of this, we can see we returned the entire third column as a vector.

6. getindex()

Another useful way to access elements within a matrix is a function called getindex. We pass values to getindex in much the same way that we did when using the square bracket notation, it just looks a bit different. We have selected the first row and the second column of the matrix stock in our getindex call, which has given us the expected value of two.

7. 2D array concatenation

One final topic to look at it is called array concatenation. This allows us to take multiple arrays and combine them into one, larger array. In this example we have two distinct two-dimensional arrays, and we want to combine them together into one large resultant matrix. To do this, we create a third matrix called concat_array, and we separate the two arrays that we want to concatenate using a semicolon, just as we did for creating a 2D array from values. Looking at the output, we can see that it stacked array_1 on top of array_2 to give us a resultant matrix that is now four by three - four rows, three columns. We can then access values in this new matrix just as we would with any other matrix, using the techniques we just discussed.

8. Let's practice!

Now that we're familiar with a whole new dimension of arrays, 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.