Vectors
Vectors (also called arrays) are lists of values which can have operations performed on them all in one go. They can be either one dimensional (like a list) or two dimensional (like a matrix), or indeed any number of dimensions. All the elements of vectors are required to be of the same type (for example all numbers or all booleans). For number vectors there is a further restriction that all elements must all have units the same dimensions.
This section describes how to create and access vectors, all functions in ODCalc support vectors and there are some specifically for managing them described in vector functions and vector creation functions sections.
Building Vectors
Simple Entry
The entering data section already showed how to enter an array using the square brackets notation, for example a list of numbers
(notice here that they were assigned to the variable aa). This could also be achieved more conveniently using the range operator.
Rows of a matrix can be entered by separating the sets of numbers by semicolons
Note that each row of a 2D matrix must have the same number of elements, as must each column (and so on for matrices with more than two dimensions).
Concatenation
The comma and semicolons above are actually able to join other vectors together, called concatenation. For example several aa matrices can be joined horizontally
or vertically
Matrix bb of the previous section could have been made more straightforwardly by vertically concatenating two matrices made via the range operator
Accessing Elements
The individual elements of a vector can be extracted using the array get operator, square brackets [ ]. These are available in four forms depending on what is put in the brackets. In each case the first element in the vector is at index 0 (this is like Java / python / C but unlike MATLAB).
Matrix Indexing
Individual elements from an array or matrix can be accessed by specifying the row and column in the square brackets, separated by commas. For example to get the third element on the second row of the matrix bb of the previous section use
remembering that the indexes start from 0, so the first element on the first row is at [0,0]:
Note that each index value must be between 0 (inclusive) and the size of the matrix in that dimension (exclusive)
Linear Indexing
Elements can also be accessed using a single index which works down each column in order, called linear indexing. This can be particularly useful applying processes to each element. For example working through some elements of the matrix bb from the previous section
In each case the index value must be between 0 (inclusive) and the length of the matrix (exclusive)
Vector Linear Indexing
It is also possible to use the linear access method with a vector of indexes to get several elements at once. For example to get the first three elements from vector aa
the range operator could have also been used there
The values are returned in a vector with the same size as indexes vector, for example a square matrix of the first two and last two elements of aa
Logical Indexing
Vectors can also be accessed using logical indexing, where the square brackets takes a boolean vector of the same size as the vector being accessed and only those elements that are true in the boolean vector are returned. For example to get the first and last elements of the vector aa from the previous section the following could be used
Logical indexing is particularly useful when using comparison operators, for example to get all the elements that are less than five in a set of random numbers between zero and ten (e.g. these could be experimental results):
Setting Elements
Once a vector has been created the values stored in its elements can be changed using expressions similar to the access expressions. In most cases the calculator allows vectors to grow in size if a value is assigned to an element that doesn't already exist. While this is convenient it is also makes the calculation slow because the whole vector must be copied into a new larger vector to accommodate the new value – if possible it is better to make a vector that can accommodate all the elements required to start with, for example using one of the zeros, ones, etc., functions.
Matrix Indexing
As with the matrix indexing access syntax, separate the row number and column number with commas. For example if zz is a square matrix of zeros
Then the middle element (second row, second column) can be set using square brackets
(remember that the first row is at index 0). The matrix could be extended to a 4x4 matrix by indexing the fourth row and column
Linear Indexing
Linear indexing, where a single number counts down the columns in order, can also be used. For example setting the top three elements a 3x3 zero matrix
Linear indexing can only be used to expand an existing vector if it is a column or row vector (otherwise the new shape is undefined)
Vector Linear Indexing
Again a vector of linear indices can be used to set elements in a vector. The new value can either be a scalar value in which case all the elements indexed are set to that value
or it can be a vector with the same size as the indexing vector in which case the indexes are set to the corresponding values from the array
Logical Indexing
Finally values can also be set with logical indexing. In this case the value to be set to must be a scalar, and all the elements that are true in the indexing logical array are set to this value. For example if xx is a matrix containing the numbers one to nine
then all the values less than five could be made zeros using the following expression