The Simulation of Physical Systems

LabView Tutorial 5 – Arrays

An array is a collection of data elements. Data elements are like storage slots for data. The data elements in an array can be numeric, boolean, string or cluster, but all data elements must be of the same type within an array. Arrays can be one or two-dimensional.

One Dimensional Arrays

One Dimensional arrays have elements and indices. Below is a 10-element, one-dimensional array. Notice that the first element has index 0, the second element has index 1, etc.

Creating a One Dimensional Array

Simple arrays are created by combining an array shell with a numeric, boolean, string or cluster data object (for our discussions, all data types will be numeric). To place an empty array shell, pop up in the Front Panel > Controls > Array & Cluster > Array

You will see the empty array shell in the Front Panel and it’s terminal in the Block Diagram:

Notice that the array terminal looks empty to indicate it is still just an empty shell.

Next, drag a data object (in this case a numeric control) into the array shell. The array shell will automatically adjust to the data object’s data type. You will see the array terminal change to indicate the data type of the array.

Manually Adding and Reviewing Array Content

By default you can only see the first array element. In order to see more elements of the array, select and stretch the array to the right. Make sure you get the frame corners or the one element will just stretch:

The above is showing the first five elements of this array. All the elements are empty. To manually enter data into the elements, select the elements one at a time and enter the data. In the example below, the data entered is the same value as the indices from index 0 – 4. To manually scroll through the array, click on the index display and increase/decrease its value. Notice that the current element is always on the left side of the array row:

Two Dimensional Arrays

Two-dimensional arrays require two indices: a row index and a column index. These are used like addresses to “look up” data elements in the array. Both indices are zero-based as in one-dimensional arrays. Below is a five-row, seven-column array which can hold 35 data elements.

Creating a Two Dimensional Array

You create a two-dimensional array by modifying a one-dimensional array. Pop up on the index display > Add Dimension. Then re-size the array display vertically to display the columns and manually add data as described above.

Creating One-Dimensional Arrays With a Loop

Manually creating and adding data to arrays can be tedious work. So loops are often used to create one-dimensional and two-dimensional arrays. Both For Loops and While Loops can create arrays and their boundaries automatically with auto indexing. For Loops are most commonly used to create arrays.

Below is an animated example of a For Loop auto-indexing a six-element, one-dimensional array. The element display has been re-sized to show a seventh element. Notice that the seventh element does not get filled and that all elements get filled at the end of the last loop iteration:

Wire Tunnels

Also notice that when a wire is brought outside a loop a Tunnel is created. In this case we want the Array Indicator outside the loop because we want the data at the end of all loop iterations.

Creating Two-Dimensional Arrays with Nested Loops

You can create nested For Loops to create a 2D array. The outer loop creates the row elements and the inner loop creates the column elements. The animated example below creates a six-column, eight-row 2D array filled with random numbers. The element display has been re-sized to show seventh column and fifth row elements. Notice that they are not filled with data and that all elements get filled at the end of the last loop iteration:

Clearing Array Data

To clear an array of data, pop-up on any Index Display > Empty Array:

Array Functions

Arrays can also be created and controlled by Array Functions. They are all found by popping-up in the Block Diagram > Functions > Array:

Array Size

The Array Size Function returns the number of elements in an array it is wired to.

Array Subset returns a portion of an array starting at index and containing length elements.

Polymorphism

Definition

Labview arithmetic functions like Add, Multiply, Divide. Etc. are polymorphic. This means that the data structures on their inputs can be either scalar or array. If any inputs are of the array data structure, the output will be an array data structure. If the inputs have array structures of different sizes, the output will be an array that is the same size as the smallest input array.

Real A x B

A is the first matrix. The number of columns in A must match the number of rows in B and must be greater than zero: k>0. If the number of columns in A does not match the number of rows in B, the VI sets A x B to an empty array and returns an error.

B is the second matrix. If the number of rows in B does not match the number of columns in A, the VI sets A x B to an empty array and returns an error.

A x B is the matrix containing the result of the matrix multiplication A x B.

Complex A x B

A is the first matrix. The number of columns in A must match the number of rows in B and must be greater than zero. If the number of columns in A does not match the number of rows in B, the VI sets A x B to an empty array and returns an error.

B is the second matrix. If the number of rows in B does not match the number of columns in A, the VI sets A x B to an empty array and returns an error.

A x B is the matrix containing the result of the matrix multiplication A x B.

A x Vector

A is the input matrix. The number of columns in A must match the number of elements in Vector and must be greater than zero. If the number of columns in A does not match the number of elements in Vector, the VI sets A x Vector to an empty array and returns an error.

Vector is the input vector.

A x Vector is the output vector containing the result of A multiplied by Vector.

Complex A x Vector

A is the input matrix. The number of columns in A must match the number of elements in Vector and must be greater than zero. If the number of columns in A does not match the number of elements in Vector, the VI sets A x Vector to an empty array and returns an error.

Vector is the input vector.

A x Vector is the output vector containing the result of A multiplied by Vector.

Complex Vector x A

Vector V’ is the input vector.

Matrix A is the input matrix. The number of rows in Matrix A must match the number of elements in Vector V’ and must be greater than zero. If the number of rows in Matrix A does not match the number of elements in Vector V’, the VI sets V’ x A to an empty array and returns an error.

V’ x A is the output vector containing the result of Vector V’ multiplied by Matrix A.

Vector x A

Vector V’ is the input vector.

Matrix A is the input matrix. The number of rows in Matrix A must match the number of elements in Vector V’ and must be greater than zero. If the number of rows in Matrix A does not match the number of elements in Vector V’, the VI sets V’ x A to an empty array and returns an error.

V’ x A is the output vector containing the result of Vector V’ multiplied by Matrix A.

Source:

A x B Details

If A is an n-by-k matrix and B is a k-by-m matrix, the matrix multiplication of A and B, C = AB, results in a matrix, C, whose dimensions are n-by-m. Let A represent the 2D input array A, B represent the 2D input array B, and C represent the 2D output array A x B. The VI calculates the elements of C using the following equation.

where n is the number of rows in A, k is the number of columns in A and the number of rows in B, and m is the number of columns in B.

Note The A x B VI performs a strict matrix multiplication and not an element-by-element 2D multiplication. To perform an element-by-element multiplication, you must use the Multiply function. In general, AB BA.

A x Vector

If A is an n-by-k matrix, and X is a vector with k elements, the multiplication of A and X, Y = AX, results in a vector Y with nelements. The VI calculates the elements of Y using the following equation.

where Y is the output A x Vector, A is the input matrix A, X is the input Vector, n is the number of rows in A, and k is the number of columns in A and the number of elements in Vector.

Vector x A

If X’ is a row vector with n elements and A is an n-by-k matrix, the multiplication of X’ and A, Y = X’A, results in a row vector Y’ with k elements. The VI calculates the elements of Y’ using the following equations.

and

X‘=[x0x1 … xn–1]

Y‘=[y0y1 … yk–1]

where Y’ is the output V’ x A, X’ is the input Vector V’, A is the input matrix A, n is the number of elements in Vector V’ and the number of rows in A, and k is the number of columns in A.

Source:

Wiring Two Arrays to the Multiply Function Does Not Result in Correct Matrix Multiplication

Problem:
If I wire two arrays, two vectors, or a vector and an array to the Multiply function, the output does not reflect proper matrix multiplication. Why does this occur?

Solution:
The Multiply function performs simple multiplication, not matrix multiplication. The function performs per-element multiplication and multiplies the elements in one array by the corresponding elements in the other array. For example, if the first element of array A is 4 and the first element of array B is 2, wiring these two arrays to the Multiply function results in an array that is the same size as A and B whose first element is 4*2 = 8.

If you want to multiply two matrices to get the correct matrix multiplication, you must use the A x B VI. This VI can be found by navigating to Mathematics»Linear Algebra in the Functions palette.