ODCalc
Vectors

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

aa = [1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]

(notice here that they were assigned to the variable aa). This could also be achieved more conveniently using the range operator.

aa = 1:5
[1, 2, 3, 4, 5]

Rows of a matrix can be entered by separating the sets of numbers by semicolons

bb = [1, 2, 3, 4, 5; 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5; 6, 7, 8, 9, 10]

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

[aa, aa, aa]
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]

or vertically

[aa; aa; aa]
[1, 2, 3, 4, 5; 1, 2, 3, 4, 5; 1, 2, 3, 4, 5]

Matrix bb of the previous section could have been made more straightforwardly by vertically concatenating two matrices made via the range operator

bb = [1:5; 6:10]
[1, 2, 3, 4, 5; 6, 7, 8, 9, 10]

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

bb[1,2]
8

remembering that the indexes start from 0, so the first element on the first row is at [0,0]:

bb[0,0]
1

Note that each index value must be between 0 (inclusive) and the size of the matrix in that dimension (exclusive)

size(bb)
[2, 5]
bb[0,5]
Array index out of bounds: index=5, size[1]=5

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

bb[0]
1
bb[1]
6
bb[2]
2
bb[9]
10

In each case the index value must be between 0 (inclusive) and the length of the matrix (exclusive)

length(bb)
10
bb[10]
Array index out of bounds: index = 10, length = 10

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

aa = 1:5
[1, 2, 3, 4, 5]
aa[ [0,1,2] ]
[1, 2, 3]

the range operator could have also been used there

aa[ 0:2 ]
[1, 2, 3]

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

aa[ [0, 1; 3, 4] ]
[1, 2; 4, 5]
aa[ [0, 1; length(aa)-2, length(aa)-1] ]
[1, 2; 4, 5]
aa[ [0, 1; length(aa)-[2,1] ] ]
[1, 2; 4, 5]

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

aa[ [true, false, false, false, true] ]
[1, 5]

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):

rr = floor( 10*rand(1,10) )
[5, 8, 7, 3, 8, 9, 5, 9, 4, 3]
rr[ rr<5 ]
[3, 4, 3]

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

zz = zeros(3)
[0, 0, 0; 0, 0, 0; 0, 0, 0]

Then the middle element (second row, second column) can be set using square brackets

zz[1,1] = 7
[0, 0, 0; 0, 7, 0; 0, 0, 0]

(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

zz[3,3] = 8
[0, 0, 0, 0; 0, 7, 0, 0; 0, 0, 0, 0; 0, 0, 0, 8]

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

zz = zeros(3)
[0, 0, 0; 0, 0, 0; 0, 0, 0]
zz[0] = 1
[1, 0, 0; 0, 0, 0; 0, 0, 0]
zz[3] = 10
[1, 10, 0; 0, 0, 0; 0, 0, 0]
zz[6] = 100
[1, 10, 100; 0, 0, 0; 0, 0, 0]

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)

aa = 1:5
[1, 2, 3, 4, 5]
aa[5] = 100
[1, 2, 3, 4, 5, 100]

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

yy = zeros(1,7)
[0, 0, 0, 0, 0, 0, 0]
yy[ 0:2:6 ] = 99
[99, 0, 99, 0, 99, 0, 99]

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

yy = zeros(1,7)
[0, 0, 0, 0, 0, 0, 0]
yy[ 0:2:6 ] = [66, 77, 88, 99]
[66, 0, 77, 0, 88, 0, 99]

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

xx = [1:3;4:6;7:9]
[1, 2, 3; 4, 5, 6; 7, 8, 9]

then all the values less than five could be made zeros using the following expression

xx[ xx<5 ] = 0
[0, 0, 0; 0, 5, 6; 7, 8, 9]

See Also

Getting Started Entering Data Units Operators Accessing Open Data Vectors Functions