VI High 27: Array Polymorphism and How to Use the Build Array Function in LabVIEW

In VI High 25 and 26 we started manipulating some 1D and 2D arrays with the Array Size, Add Array Elements, and Index Array functions. Now we’re going to take a look at array polymorphism and then the Build Array function. How do I combine two arrays in LabVIEW? How do I concatenate them? Watch and learn!

For more on programming in LabVIEW, check out the Sixclear LabVIEW Fundamentals course at www.sixclear.com/labviewtraining/.

Experience level: Basic

(start transcription)

Now we’ve seen these functions change based on what we wire into them. Why do they do that? Well, that’s called polymorphism. That’s right, polymorphism. And if you take the CLAD exam, you’ll see it on there. Let me be explicit. Polymorphism. Look at how important that is.

It’s just the ability of the functions to change their functionality based on the inputs that are wiring in. Go ahead and look it up in the LabVIEW help, or check out the Sixclear LabVIEW Fundamentals course. We talk about it there. Anything else valuable in that array palette? Oh yeah, there’s a ton. A lot of it follows the same patterns that we’ve already seen with the Index Array, manipulating the rows and columns, changing the behavior based on what we wire in, so we won’t go into every one of these. The context help is pretty valuable.

But building an array is pretty common. I can pull that down here, and I can create a new array from a variety of things. So let’s say I want to combine these two arrays. To do that, I need to expose multiple elements, so I’ll hover over the bottom, click and drag, two elements, wire this 2D array into the top, wire this 1D array into the bottom.

What comes out? Well, we create an indicator, pull it down here, expand it a bunch, run, and we see these two arrays have been combined together, with this array being added to the bottom of the existing array coming from here.

Now what if I wired in two 1D arrays? Would it do the same thing? Well, it depends. Let’s get a little crazy and create a copy of this, take this array right here, which we know is this array, and then, take this array and wire it in. Create an indicator, and you see our resulting array is actually a one dimensional array.

Of what? Well, I’ll run it, and I’ll see the first two elements from here, and then all the rest of the elements from here. So what I’ve really done is concatenate the array. Take two 1D arrays, and put them end to end. In fact, if I right click on it, I see concatenate inputs is checked, whereas down here I see it’s not. It’s not even an option, because you can’t concatenate arrays of different dimensions.

But can I create a higher dimension array from this build array function? Yes I can. I can right click on it, uncheck concatenate inputs. Now, again broken wire because this wants to create a 2D array. I’ll delete that, create an indicator, there we are. Let’s get rid of some of this other stuff. CTRL-B to clean up the broken wires, run it, and there we go. The two 1D arrays have formed a 2D array. As you see, LabVIEW padded this first array with zeroes, because it wasn’t the same size. But that’s ok.

Well, that was really fun. What will we do next? Well, next episode we’ll go about creating arrays in LabVIEW where we haven’t already manually created them on the front panel or block diagram. So we’ll use the Initialize Array function, which is pretty easy, but we’ll also do some auto-indexing through loops. So, see you next time.

(end transcription)

VI High 26: How to Use the Index Array Function in LabVIEW

In VI High 25 we were manipulating some 1D and 2D arrays with some simple functions. Now we’re looking at one of the most common array functions: Index Array. How do I grab a single element out of a 1D array? Or a row/column out of a 2D array? And what is array polymorphism? It’s time to take a couple minutes and find out.

For more on programming in LabVIEW, check out the Sixclear LabVIEW Fundamentals course at www.sixclear.com/labviewtraining/.

Experience level: Basic

(start transcription)

One of the most valuable is the Index Array function. I’ll put it down here. Why is it valuable? Well, let’s say in my code, I want to get ahold of this element and use it, maybe to multiply by another value. How do I get that out of that array?

Well, that’s the Index Array function. I take my array, wire it into the Array Input, and below I see the Index Input. Create a constant, and what number am I going to put in here? Remember that array is zero indexed, so zero, one, two. I’ll put in a two. Create an output. Run it, and indeed, there’s my value.

What happens if I take that same index array and wire a 2D array up to it? Look what happened: it changed. The context help can tell me what I’m seeing. It says it’ll return the element or subarray of a n-dimensional array. Hmm. Well, what if I were to go and put a zero in the top input?

You see I have Index Row, and Index Column. It says disabled, but that’s only because I don’t have anything wired into right now. So I’ll create a constant, and I’ll also make this two. Create an indicator, and I get a subarray out. Let’s take this subarray and pull it over here, run the VI, expand it over, and as we can see, what I’ve pulled out is this row - zero, first, second row -right here. Could I have also pulled out a column? Absolutely. I would have just wired this into the lower input.

And now look at the tip strip says Index Column, and then the row index is disabled. I run it, and indeed now I’m looking at the second column, right here. Can I go and just pull out one element from here? You know it. I’ll pull this down, hold down CTRL, click and drag to create a copy. LabVIEW auto-wires for me, and it also breaks a wire. Why does it do that?

Well, by wiring both inputs, I’ve essentially told the function that I’m going to specify both the row and column so that it should output a scalar value instead of an array. So I’ll get rid of this, and create an indicator. Let’s change this value to three, run it, so the third row down - zero, one, two, thee - and the second column over - zero, one, two - and there it is.

(end transcription)

VI High 25: How to use the Array Size and Add Array Elements Functions in LabVIEW

In VI High 24 we built some simple arrays. Now we’re going to take the next few episodes to look at some common array functions with 1-D and 2-D arrays. We’ve cut the episode length down to be bite-sized, digestible, and tasty. Bon appetit!

For more on programming in LabVIEW, check out the Sixclear LabVIEW Fundamentals course at www.sixclear.com/labviewtraining/.

Experience level: Basic


(start transcription)

Hi, it’s been a while.

In our last episode, we took a look at creating arrays on the front panel and on the block diagram. In the two step process, first we put down an empty array shell and then we populated it.

So let’s start over here with Array Size. Put it down. The context help, CTRL-H, is always invaluable for knowing exactly what these do. Returns the number of elements in each dimension of the array. Well that’s cool. So I’ll wire this in here, create an indicator, pump up the output, I see it show up here. I run my VI, and indeed I have five elements in my array.

Now what if I wanted to know the value of all the elements added together instead of just the number of elements in my array? Well, for that I would go to the Add Array Elements function. Put that down. Wire this into here. Create an indicator as before, and run it. And here’s the sum of all these elements. That’s pretty cool.

What if I use this on a 2D array right here? Hold down control, click and drag this up, I’ll get rid of this for now, and wire the 2D array into the function input. Create an indicator. Oh, look what happened. I get an array as the output. I’ll expose two elements and run it, and I get two elements out. There are no more elements, just those. And as you can see, these elements correspond to the size of this array. Four rows. Five columns.

Before we move on, let’s take a look at these wires. First off, a scalar wire is pretty skinny. A 1D array wire is thicker, and a 2D array wire is thicker still. Look what I did. I made this a 3D wire, and it got a bit bigger. And you can guess if I run it and expose another element, yep, I have the size of each dimension. This other dimension is, as yet, unpopulated. You can think of it like multiple Excel spreadsheets. I’m looking at one, and here’s the other. But for now, I’ll hit CTRL-Z and go back, and take this back to two dimensions because we’d like to look at more functions.

(end transcription)

VI High 15: How to Find and Replace Duplicates From Our Selected Random Array Elements in LabVIEW

In our last episode we made a simple VI to randomly select items from an array but we found that it was prone to finding duplicates. This time around we find out how to avoid those duplicates.

For more on learning LabVIEW, check out the LabVIEW Fundamentals Course offered by Sixclear: sixclear.com.

Experience Level: Basic

(start transcription)

“When we finished VI High #14, we had pulled out some random names from our Name Array, but we noted that we often ran into duplicates and we said that we would be coming back to item 3 in our implementation plan to find and replace those duplicates. There are, again, a few ways to do this but remember the primary characteristic of our implementation should be: robustness.

Robustness.

Let’s first be aware of some of the characteristics of the duplicates. Sometimes we only get one duplicate, or in poker terminology, a pair. Look at Sarah. Sometimes we get two pair, like 2 Aruns and 2 Veronicas. And sometimes we get three of a kind or more, like greedy Mindy here.

So we need to search through this array of indices to find these duplicates. First let’s clean up this block diagram. Then let’s use a For Loop which we’ll auto-index with this array of indices, and we want to compare this element to every other element in the array, which means we’ll bring in the entire array and disable indexing. We’ll want to remove this element we’re checking from the array. And we’ll use the Delete From Array function to do so. The iteration terminal will supply the appropriate index. So now we’ll put another For Loop inside of this one and index the array without the element we’re checking and compare the element we’re checking to every element in here with an equal function. So is this equal to this. If it’s true, then we want to replace that element in this array that we’re checking. So within a case structure, we’re essentially going to duplicate this little block of code over here except we’re doing it for a single element instead of an array. So I hold down shift and click, click, click and then hold down Ctrl, click and drag them over here. This new element I just created is going to replace the element we’re checking, the index of which is here in this iteration terminal of the outer For Loop. And so we’ll use a Replace Array Subset. I’ll hold Ctrl, click and drag to give us more space, delete this auto wiring which wasn’t helpful, wire  this array into here, disabling indexing. That’s the index, and the new value.”

“And note that the array that I use the Replace Array Subset with is the full array, not the one from which I had removed that element in question. I’ll wire the resulting array to the border, disable indexing on both of these For Loops, their output. And in the False case of the case structure, we’ll just wire this array through because we didn’t find a match and we’re not changing anything. Now we want these For Loops to keep running until they’ve run through all elements in the array or they’ve found a match. This is a perfect use case of a Conditional For Loop which is available in LabVIEW 8.5.1. and beyond. So I’ll right click on the border of the For Loop, Conditional Terminal, once again I’ll hold down Ctrl click and drag to create some space, and now I’ll wire this boolean to the Conditional Terminal. And we’ll do the same for the outer For loop. And disable indexing, we just want one value.

So what do we do now? Have we entirely fixed the duplicate problem? Well no, this implementation will fix a single pair, if we’re again invoking our poker terminology. But what about the two pair or 3 of a kind? Remember greedy Mindy. This implementation won’t address those problems so we need to take the entire array, after the replacement, and run it through the same process again. How do we do that? We’ll wrap these nested For Loops in a While Loop. Give some space, and we’ll use a shift register to send this array back to the beginning and run it through the same process again. So the input array will no longer come through this tunnel, but rather the shift register. 

And what condition will stop the While Loop? We’ll know we don’t need to run the While Loop anymore when both of these For Loops stop running and they didn’t find a match, meaning that this wire will be false. So we’ll change the Conditional Terminal on the While Loop to Continue if True, wire that value to it, disable indexing, and so the While Loop will stop when the Boolean value is false. Now the output from here will be a set of 6 unique indices. So we’ll just drag this little block of code - Ctrl click and drag - over here, and make another output: Real Winners! Excitedly. And run it.

We see that we’re successful, by checking that, in the case where there’s a duplicate in the Winners list, like Santiago, it is replaced with a unique name in the Real Winners! list. Run it a few more times, we have two Xui Lis, and only one in the Real Winners list. Fantastic.

That’s it, this VI is ready for action, which it will see after 11:59PM CST on June 9 when the sweepstakes ends. If you haven’t entered yet, get to it by visiting the instructions at news.sixclear.com. And once again, good luck!”
(end transcription)

VI High 14: How to Select Random Elements from an Array in LabVIEW

In this episode we are preparing to select the winners of our sweepstakes giveaway. What better way to make the random selection than a LabVIEW VI?
None.
Join us as we look at how to randomly select items from an array and how to avoid the pitfalls of this seemingly simple task.

For more on learning LabVIEW, check out the LabVIEW Fundamentals Course offered by Sixclear: sixclear.com.

Experience Level: Basic

(start transcription)

“Avid Sixclear paparazzi are already well aware that entry to our sweepstakes giveaway of six licenses of the Sixclear LabVIEW Fundamentals Online Course is days away from coming to a close. As such, we need a way to randomly select the six lucky winners from the eager entrant pool. What better way than LabVIEW?
Our task is well defined. We will have an array of strings with names of our entrants. From that array, we will randomly select 6 with LabVIEW. There are several ways to implement this, we’ve chosen the most hopelessly interesting.

We will implement a solution in the following manner:

1. Generate 6 random numbers
2. Scale the random numbers by multiplying them by the size of the name array, this will generate an array of indices that can be used to pull out random names from the name array
3. Check for and replace duplicate indices
4. Use the indices to generate an output array of 6 random names

Let’s head to LabVIEW and do it.

I’ve already created a multicultural array of names on the front panel that we’ll use. Generating the 6 random numbers is easy. Just put a Random Number function in a For Loop running 6 times and auto-index the output. That occurs by default. Now to scale those to the size of the name array, we’ll take an Array Size function and use the polymorphic attribute of the Multiply function to multiply the array and the scalar output together. Now these numbers coming out are extremely precise fractional numbers and we just want whole numbers corresponding to indices, so we’ll use the Round Toward -Infinity function. For now, we’ll skip step 3 and just use this array of indices to generate our output names array. We’ll auto-index this array into a For Loop and pass the index to an Index Array function. The array we’re indexing is the full Name Array, and we’ll want to disable indexing on this tunnel coming in because we want that full array. Each iteration of this loop will give us one of the selected names, so we’ll auto-index the output to generate the full list of 6 names.

If I run this a few times we see that this procedure does indeed work except that we sometimes run into duplicate indices as a result of rounding down each element. You can imagine that multiple numbers in this array could be rounded down to the same number and so we have duplicates. Obviously we have a small Name Array and so we’re more likely to get duplicates, but we need a robust way to find and eliminate those duplicates for any size array coming in. Remember, robustness.

And remember that until next time because we are out of it. Uh, that’s time, not robustness, still plenty of that. In the meantime, you can enter the sweepstakes until June 9, 2011, just check out the entry requirements on our news blog: news.sixclear.com.”

(end transcription)