Creating Vectors and Matrices

In two dimensions there are just two types of arrays: vectors and matrices. A vector is an array of just one row or column, while a matrix has multiple rows and columns. Mathcad functions that expect a vector argument generally require a column vector.

Tutorial - Vectors and Matrices

Defining an array

There are several ways to define an array. You can:

Once an array is defined in terms of a variable name, you can use that variable in any calculation where you would like to represent the array.

Indexing an array

Arrays begin with index 0 by default. This can be changed by adjusting the built-in variable ORIGIN in your worksheet. Beware of not including all elements in an array in a calculation by creating a range variable which begins after the ORIGIN, or ends before the last element of the matrix or vector. For ORIGIN = 0, the last index of a vector is length(v) − 1.

You can create an m × n matrix simply by defining the bottommost corner element with the subscript operator, for example

M[100,50 := 0

Doing so creates a 101 × 51 element matrix (assuming an ORIGIN of 0). Unspecified elements are filled with 0's. Arrays cannot contain empty elements (no sparse matrices). Be careful of inadvertently creating large arrays or arrays with unexpected 0 elements by defining elements individually out of sequence.

Contents of an array and nested arrays

Arrays can contain numbers, expressions, or strings. Arrays can also contain nested arrays. The simplest way to define an array element that is itself an array is to assign the element using the subscript operator, for example

M[1,1 := v

The element in row and column 1 of M is now the vector v. You cannot use the Insert Matrix dialog box to create a nested array when the selected placeholder is already inside a matrix.

You can display nested matrices inside matrices, and view matrix content either as a traditionally drawn matrix or a table.

QuickSheet: Nested Arrays

Changing Size of a Vector or Matrix

You can insert or delete rows and columns:

  1. Click on one of the matrix elements to place it between the editing lines. Mathcad begins inserting or deleting with this element
  2. Choose Matrix from the Insert menu. Type the number of rows and/or columns you want to insert or delete. Then press either "Insert" or "Delete."

Note:

Array Size and Memory Management

The most efficient way to use your computer's memory when working with arrays is to preallocate the array. This can easily be done by assigning the last element of the array to 0, as described above in Indexing. This sets aside enough space in memory for the rest of the array. Dynamically assigning arrays, that is, filling them up from the smallest index to the largest, takes more computational time, and can slow down your worksheet. This is true both at the region level in the worksheet and locally inside programs.

The array size limit depends on the memory available on your system. Approximately 8 bytes of memory are needed per array element. For most systems, the array limit is at least 1 million elements. Note that the maximum allocatable memory in a 32 bit system is 4 Gb, which has to hold all open applications, your operating system, and whatever memory is consumed by your calculations.

You can conserve memory by allocating intermediate arrays (whose values are a step in a calculation and not the result) inside a program, which creates the array temporarily and frees the memory when the program exits. Every new array defined in your worksheet at the top level remains in memory until the worksheet is closed.

Related Topics