Multi-dimensional array basics
In GAUSS, the matrix is the fundamental data type. If you have been using GAUSS for any length of time, using matrices should be familiar. In this tutorial, we will explain the usage of multi-dimensional arrays as a set of matrices. If you are comfortable working with matrices, shifting to multi-dimensional arrays should not be much of a leap.
Arrays are really just a group of matrices
To make our illustration as simple as possible, we will start out by making two 4×3 matrices from an additive sequence and then we will create a 2x4x3 array with the same data. Here are the matrices:
//Create a column vector with the sequence 1,2,3...24 data = seqa(1, 1, 24); //Create a 4x3 matrix with the first half of 'data' x1 = reshape(data, 4, 3); //Create a 4x3 matrix with the second half of 'data' x2 = reshape(data[12:24], 4, 3);
After this code snipppet, x1 and x2 contain the following values:
1 2 3 x1 = 4 5 6 7 8 9 10 11 12 12 13 14 x2 = 15 16 17 18 19 20 21 22 23
Now we will accomplish the same thing with one 2x3x4 array.
//Create a column vector with the sequence 1,2,3...24 data = seqa(1, 1, 24); //Create a 2x4x3 array from 'data' a = areshape(data, 2|4|3);
After this code if we print a you will see:
Plane [1,.,.] 1 2 3 4 5 6 7 8 9 10 11 12 Plane [2,.,.] 13 14 15 16 17 18 19 20 21 22 23 24
As you can see, the 2x4x3 array contains the same data as two 4×3 matrices and for many that is the simplest way to think about them.
Indexing multi-dimensional arrays
Multi-dimensional arrays are indexed in GAUSS the same way that matrices are indexed, using square brackets . Scanning above, you can see that the value of the element at the intersection of the third row and second column of x1 is 8. Using matrix indexing, you could assign this value to a variable e like this:
e = x1[3, 2];
To extract this same element from the multi-dimensional array a will be almost exactly the same. If you think of a as being two 4×3 matrices, inside the square brackets, you will need to specify which of the two matrices as the first index and then the final two numbers will be an index into that matrix just like for x1.
e = a[1, 3, 2];
The last element in a is located at the intersection of the fourth row and third column of the second matrix. To assign this value a variable last_elem:
last_elem = a[2, 4, 3];
Now, last_elem should be equal to 24.
Indexing rows, columns and matrices
GAUSS uses a dot inside of square brackets to indicate all elements. For example to extract the first column of x2:
first_col = x2[., 1];
The notation above tells GAUSS to grab all rows from the first column. If you wanted to extract the same data from a (the first column of the second matrix of a), you could:
afirst_col = a[2, ., 1];
After executing both code snippets above, both first_col and afirst_col should be equal to:
13 16 19 22
To extract the entire second matrix from a you simply place the dot in the row and column position like this:
new_x2 = a[2, ., .];
Indexing multiple rows and/or columns with the colon operator
After working through the examples above and doing some experimental indexing of your own, you should be able to extract any row, column or matrix from a multidimensional array. What we have not covered is extracting a range of data that is less than all of the columns, or all of the rows.
GAUSS uses the colon operator : to indicate a range. Let us say that you wanted to extract the third and fourth rows of the second matrix in a. Using what you have learned above and the colon operator, you can accomplish this goal using this line of code:
two_rows = a[2, 3:4, .];
This code will assign two_rows to be equal to:
19 20 21 22 23 24
If you decided that you really only wanted the first and second columns from the third and fourth rows of the second matrix in a or:
19 20 22 23
You could restrict the last indexing statement to only the first and second columns using the colon operator like this:
two_partial_rows = a[2, 3:4, 1:2];