How to Specify Arrays in Statistica Visual Basic Functions

Statistica Visual Basic supports the use of arrays (collections of related values) to organize your data. You can declare an array of up to 8 dimensions (at the beginning of the program) by using the Dim statement. The most common kinds of arrays are one and two dimensional arrays.

One dimensional arrays. One dimensional arrays are also known as a vectors. You can define a vector using the Dim (or ReDim) statement as follows:

Dim A(5) As Double

The above definition creates an array named A which has 5 double precision values. Each value in an array is called an element, and these elements are referenced by a numeric index. In the above example, the individual elements of array A can be referenced by the following convention: A(i) where the subscript i represents the element number (e.g., A(0) references the first element while A(5) references the sixth element in the array). For example, you can assign a value of 6 to the fourth element in the array A with the following statement:

A(3) = 6

The following example demonstrates creating an array, resizing it to the size of a 10x10 spreadsheet, filling it with numbers, and copying its contents to a new spreadsheet:

'Create a dynamic array
 Dim Matrix() As Double
 'Resize matrix to the size of a 10*10 spreadsheet
 ReDim Matrix(10,10)
 'fill the matrix
 'the first loop handles going through the rows
 For i = 1 To 10

'this loop handles going through the columns
 For j = 1 To 10

Matrix(i,j) = i

Next j

Next i
 'create a spreadsheet
 Dim s As New Spreadsheet
 'fill spreadsheet with the matrix
 'the first loop handles going through the rows
 For i = 1 To 10

'this loop handles going through the columns
 For j = 1 To 10

s.Cells(i,j) = Matrix(i,j)

Next j

Next i
 s.Visible = True

Two dimensional arrays. A two dimensional array is referred to as a matrix. You can easily create a two dimensional array using either the Dim (or ReDim) statements. For example:

Dim B(3,2)

The resulting array is 4 rows by 3 columns. The elements in B are referenced with two subscripts B(i,j). Here, i represents the row element and j represents the column element. The following program segment shows how you might reference the elements of a two dimensional array in order to assign specific values to them:

Dim B(2,2)
 'The first column is all zeros
 B(0,0) = 0
 B(0,1) = 0
 B(0,2) = 0
 'The second column is all ones
 B(1,0) = 1
 B(1,1) = 1
 B(1,2) = 1
 'The third column is all twos
 B(2,0) = 2
 B(2,1) = 2
 B(2,2) = 2

See also, Array Indexing and Statistica Visual Basic Library of Matrix Functions - ReDim.