II. Arrays and Matrices

What is an Array?

An array (in Z-Tree) is a collection of values each identified by an integer index. All arrays are stored in  table scope they were declared in. You may already be familiar with arrays in mathematical constructs such as vectors and single dimensional matrices.

Below is a graphical depiction of an array containing seven subject payoffs.

 

In the above example, the index for each element of the array is placed above the element. Elements of an array can be referenced by placing the index between two brackets preceded by the array name. In the above example the payoff for a subject  can be retrieved (or more appropriately dereferenced) by specifying the array name postfixed by the index of interest surrounded by brackets. Thusly, to retrieve the payoff for subject two the following syntax would be required: Subject_payoffs[2], which as illustrated above contains a payoff of 4. Likewise the payoff for subject six can be retrieved by Subject_payoffs[6], which references a payoff of 8.

 

Array Declaration

ExampleTable.do {
    array array_name[array_size];
}

The above declaration creates an array in the SyntaxExample table of size array_size. The first element of the array can be retrieved using index value 1, and the last using index value array_size.

Example

Subjects.do {
    array payoff[3];
}

In the above example an array is created consisting of three elements, all of which are initially empty. In the abstract, nothing is represented as the absence of a value — as illustrated. Yet, in Z-Tree when an array is declared all of the elements are initialized to zero and placed in its corresponding table, as defined by its scope.

 

Array Initialization

 Syntax

ExampleTable.do {
    superlative_array[selection_index] = some_value;
}

The above syntax illustrates the initialization superlative_array element, referenced by the selection_index value, which is set to some_value.

Example

Subjects.do {
    num_of_rounds = 3;
    array payoffs[num_of_rounds];
    payoffs[1] = 7;
    payoffs[2] = 8.25;
    payoffs[3] = 4.5;
}

In the above example a array named payoffs is declared of size num_of_rounds, after which each element is initialized with a number.

What is a Matrix?

A matrix, in this implementation, is defined to be a 2-dimensional array indexed by row and column.  Matrices are particularly useful when analyzing or operating on sets of data.

The Z-Tree scripting language does not have a matrix (or multi-dimensional array) data type, as in other scripting and programming languages. Yet it is possible to implement a matrix by manipulating the index reference of an array. The index function for doing so is as follows:

The above index function can be used for a M by N matrix; where r is the row number, c is the column number, and n is the number of columns in the matrix. Where r and n are expected to be greater than or equal to zero, and n greater than zero.

 

Constructing a Pseudo Matrix

Matrix Initialization

The above illustrates what will be implemented in code below. That is, a m by n matrix (in this case a 3 by 5 matrix) is separated by row and re-joined contiguously in row order to form an array. The pseudo matrix is index as an array (in grey below the bottom most array), yet the column and row constraints (shown above, respectively) can be preserved though the use of the index function.

Example

Subjects.do{
    rows = 3;
    columns = 5;
    row_by_column = rows * columns;
    array pseudo_matrix[row_by_column];
}

In the above example the pseudo_matrix is initialized (in the subjects table) containing row_by_column elements, in this case 15.

Matrix Utilization

To retrieve the elements of the pseudo matrix the index function must be used to map the matrix row and column index to an array index. In the above illustration the index function is used to calculate the index for the element in row 1 column 3, row 2 column 3, and row 3 column 3 — respectively. All of which can be implemented in Z-Tree as follows:

Subjects.do{
    rows = 3;
    columns = 5;

    array_size = rows*columns;
    array pseudo_matrix[array_size];
    
    row_one_column_three = columns*(1-1)+3;
    row_two_column_three = columns*(2-1)+3;
    row_three_column_three = columns*(3-1)+3;
}

The power of arrays and matrices can truly be leveraged with the aid of loops, which will be discussed in the next section.

Next > Condition statements and Loops