Lib4U

‎"Behind every stack of books there is a flood of knowledge."

The Simulation of Physical Systems

labview_0149

LabView Tutorial 5 – Arrays

Arrays

Definition

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.

A 10 element array


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

The 'Array' sub-menu

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

An empty Array Shell on the Front Panel The Block Diagram graphic of an array

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.

Animated gif showing the dragging of a data object into an Array Shell


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:

Animated gif streching the Array Shell to show more elements

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:

Animated gif - scrolling through the array manually


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.

An image showing a table, a 5-row, seven-column two-dimensional array


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.

Right-click on the Array Shell to pop-up the 'Array' menu with the 'Add Dimension' option


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:

Animated gif of a 'For' loop auto-indexing a two-dimensional array
Block Diagram for the auto-indexing of the two-dimensional array

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:

Animated gif showing the creation of a two-dimensional array of random numbers
Block Diagram for the two-dimensional array creation

Clearing Array Data

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

Front Panel of a two-dimensional 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:

The 'Array' sub-menu from the 'Functions' menu of the Block Diagram

Array Size

The 'Array Size' node

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

Image showing the array and the number generated from the 'Array Size' function

Initialize Array

The 'Initialize Array' icon

Initialize Array creates an array of dimension size elements containing the element value.

Image showing how the array is initialized via the 'Initialize Array' function

Build Array

The 'Build Array' icon

Build Array links multiple arrays. It appends elements to an array.

Image showing how 'Build Array' works

And it concatenates multiple arrays

Another image showing how 'Build Array' works

Array Subset

The 'Array Subset' icon

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

Image showing how 'Array Subset' works


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.

An image showing the result of different scalar/array or array/array combinations

Source:

http://attila.sdsu.edu/me205/modules/1labview/05arrays/2tutorial.html

A x B VI

LabVIEW 2011 Help

Edition Date: June 2011

Part Number: 371361H-01

»View Product Info

Owning Palette: Linear Algebra VIs

Requires: Base Package

Performs the multiplication of two input matrices or an input matrix and an input vector. The data types you wire to the A and B inputs determine the polymorphic instance to use.

(FPGA Module) To multiply a matrix by a vector on an FPGA, use the Matrix*Vector Express VI.

Details

Use the pull-down menu to select an instance of this VI.

       Select an instance       Real A x BComplex A x BA x VectorComplex A x VectorComplex Vector x AVector x A

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.
error returns any error or warning from the VI. You can wire error to the Error Cluster From Error Code VI to convert the error code or warning into an error cluster.

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.
error returns any error or warning from the VI. You can wire error to the Error Cluster From Error Code VI to convert the error code or warning into an error cluster.

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.
error returns any error or warning from the VI. You can wire error to the Error Cluster From Error Code VI to convert the error code or warning into an error cluster.

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.
error returns any error or warning from the VI. You can wire error to the Error Cluster From Error Code VI to convert the error code or warning into an error cluster.

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.
error returns any error or warning from the VI. You can wire error to the Error Cluster From Error Code VI to convert the error code or warning into an error cluster.

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.
error returns any error or warning from the VI. You can wire error to the Error Cluster From Error Code VI to convert the error code or warning into an error cluster.

Source:

http://zone.ni.com/reference/en-XX/help/371361H-01/gmath/a_x_b/

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 Ak 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 Vectorn 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‘=[x0 x1 … xn–1]

Y‘=[y0 y1 … yk–1]

where Y’ is the output V’ x A, X’ is the input Vector V’, A is the input matrix An 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

 

Primary Software: LabVIEW Development Systems
Primary Software Version: 5.0.1
Primary Software Fixed Version: N/A
Secondary Software: N/A

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.

Related Links:
LabVIEW Help: A x B VI
KnowledgeBase 2PNGS2A2: Why Does my Solve Linear Equations VI Give The Wrong Solution Vector?

Attachments:
Matrix Multiplication.vi

Source:

http://digital.ni.com/public.nsf/allkb/862567530005F09C862566F400818DC0

Leave a comment

Image

Virtual Fashion Technology

Virtual Fashion Education

toitocuaanhem

"chúng tôi chỉ là tôi tớ của anh em, vì Đức Kitô" (2Cr 4,5b)

Theme Showcase

Find the perfect theme for your blog.

lsuvietnam

Learn to Learn

Gocomay's Blog

Con tằm đến thác vẫn còn vương tơ

Toán cho Vật lý

Khoa Vật lý, Đại học Sư phạm Tp.HCM - ĐT :(08)-38352020 - 109

Maths 4 Physics & more...

Blog Toán Cao Cấp (M4Ps)

Bucket List Publications

Indulge- Travel, Adventure, & New Experiences

Lib4U

‎"Behind every stack of books there is a flood of knowledge."

WordPress.com News

The latest news on WordPress.com and the WordPress community.