A vector is an array of numbers designed for high-performance computations. For a full list and details of functions and operators on vectors, see Functions and Operators on Vectors.
Vector basics
Vectors serve as a way to perform computations with diverse data types. For instance, arrays of values can be extracted as vectors from data types such as fieldset
, geopoints
, netcdf
, odb
and table
. Some of these types also allow their arrays to be set using vectors.
Vectors can also be passed to inline Fortran or C/C++ code for further processing.
Vectors are created using the vector() function and matrices with the matrix() function. Their elements are read or set using the [] operator :
# Allocate a vector of 5 elements
v = vector(5)
# Initialise their value
for i = 1 to 5 do
v[i] = i
end for
Vector literals can be written using the | character :
v = |3,6,7,9,10|
How operators and functions work on vectors
Operations between vectors and vectors are carried out between each pair of corresponding vector values. The result is another vector. Thus :
Z = X+Y
is equivalent to :
for each value i
Zi = Xi + Yi
If one operand is a scalar and the other a vector, the operation is carried out between each vector value and the scalar. The result is another vector. Thus :
Z = X+n
is equivalent to :
for each value I Zi = Xi + n
The same logic applies to functions. If the argument of a function is a vector, the result is a vector where each element is the result of the function at the corresponding element in the input vector. Thus :
Z = f(X)
is equivalent to :
for each value i Zi = f(Xi)
Boolean operators such as > or <= produce 0 when the comparison fails, or 1 if it succeeds. Thus :
Z = X > 0
gives a vector where all the values are either 1 or 0 depending on the corresponding values of the vector X being above 0 or not.
Indexing vectors
Indexing a vector allows you to access particular elements inside it. Indexing uses the square bracket operator []. At its simplest you can use it to extract or refer to a single value inside a vector.
X[i] = ith value of vector X:
# copies element 2 of vector X into Y
Y = X[2]
More sophisticated usage of [] allows you to extract or refer to a range of values.
x[i,j] = all values of vector X from the ith to the jth :
# copies values 3, 4, 5, 6, 7 and 8 of X into Y
Y = X[3,8]
X[i,j,k] = every kth value of vector X, from the ith to the jth :
# copies values 1, 5, 9, 13, 17 of X into Y
Y = X[1,20,4]
An additional fourth parameter specifies how many elements to extract from the current step :
# copies values 1,2, 5,6, 9,10, 13,14, 17,18 of X into Y
Y = X[1,20,4,2]
If a vector is holding data representing a rectangular structure, this form could be used to extract a 'sub-area'.
Additionally, it is possible to assign a vector to an indexed position in another vector, for example: v[4] = |99,99,99|
. In this example, elements 4, 5 and 6 of v
will be replaced.
Missing Values in Vectors
Vectors can contain missing values. These can be assigned or tested for using the global variable vector_missing_value
. Operations between vectors will bypass missing values. For example, if we represent a missing value with an 'x', then the result of
|1,2,3,x,5| + |2,2,2,2,x|
will be
|3,4,5,x,x|
See the descriptions for particular functions and operators for specific details. The bitmap()
function can be used to translate between missing values and 'real' values. When a vector is printed with the print()
function, missing values are represented by an 'x
'.
When a vector is generated from a fieldset, e.g.
a = values(fieldset)
missing values from the field are automatically translated into missing values in the vector. The same is true when obtaining a vector of values from a geopoints variable. Missing values in vectors are also translated correctly when inserted into fieldsets and geopoints.