LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Using an array as a case selector

Solved!
Go to solution

Hi,

 

I'm trying to make a random number generator, and write a file with the random numbers but only if all  the numbers from the array meet a certain condition (>.3). Currently the Vi works when there is no condition but trying to plug the outcome of the 'Greater or equal' to the case selector gives me an error saying that the source and the sink types do not match. Is there a way to use this outcome as a case selector?

 

Thanks in advance,

Robert

0 Kudos
Message 1 of 9
(1,618 Views)

You now have an array of booleans.  If you want them ALL to be true, then use AND Array Elements.  That will give you a single True when all the elements are true.

Message 2 of 9
(1,613 Views)

Hi. The problem here is that you are trying to connect a boolean array in the case selector. There is no way to connect an array into the case selector. So, as a solution you can change the design of your corde a bit. Try to get the minimum of your array using the "max/min" function, then if the minimum is greather than your setpoint (e.g. 0.3) so you decide whether to save it or not.

0 Kudos
Message 3 of 9
(1,605 Views)
Solution
Accepted by topic author robertomp09

First of all, your array diagram constant (lower input of the comparison) is an empty array (and having a default element values of 0.3 does not change that). This means that the output of the comparison is guaranteed to be an empty array (the smallest array always wins!)

 

You need to make two changes (see picture):

  • Change the array diagram constant to a scalar.
  • Turn the result into a scalar by "and array elements".

Compare.png

 

From a casual glance, there are plenty more things wrong with this code. (1) Why would you use "insert into array" instead of "build array"? (2) Concatenating empty strings does not seem very productive. (3) etc. etc. Maybe continue with a few more tutorials before proceeding?

 

It would also be much safer to stream the matching data to a file instead of building an ever-growing string array in memory and write it to file only after the main loop stops. If the computer would crash, you'll lose all data!

 


LabVIEW Champion. It all comes together in GCentral GCentral
What does "Engineering Redefined" mean??
0 Kudos
Message 4 of 9
(1,580 Views)

I can't see your code (because I don't have LabVIEW 2017 installed on my laptop -- long story), but I wonder (not having seen the Assignment) if you are "solving the wrong problem".  It sounds like you Create an Array of N Random Numbers, and Accept the Array if all the numbers are >0.3.  Let's say N = 4.  The probability that 4 U(0,1) Random Numbers will have one <0.3 is 75% (from my "back of the envelope calculation), so you'll (on average) throw away 3 arrays for every one that succeeds.  Let N get much larger and it could take a long time to find a satisfactory array.

 

How about generating a U(0,1) (this means a random number Uniformly Distributed between 0 and 1, what LabVIEW's Random Number function gives you) and only adding it to the Array if it is <0.3?  Again, with N=4, it should take you about 5 tries to get 4 good numbers.

 

You would not use a For Loop, but would use a While Loop, with an indexing tunnel on the output to build your array.  If you right-click this tunnel, you can add a Conditional Terminal to it, which lets you specify that you add your Random Number only if it is >0.3.  You exit the Loop when the number of successful Random Numbers = the desired size of the Array.

 

I've said this all in words, rather than give you a VI, as I'm guessing this is a Class Assignment, or you are otherwise trying to learn LabVIEW.  Doing it yourself from what I hope are clear suggestions will make it "stick" in your mind.

 

Bob Schor

0 Kudos
Message 5 of 9
(1,547 Views)

Or generate a random number between 0.3 and 1 !

0 Kudos
Message 6 of 9
(1,539 Views)

@RavensFan -- Duhhhh.  Why didn't I think of that?

 

BS

Message 7 of 9
(1,518 Views)

@RavensFan wrote:

Or generate a random number between 0.3 and 1 !


I suspect you are just trying to be funny here 😄

 

Since you did not add any smileys, I'll make a serious comment.

 

Well, the gist of the exercise here is supposed to be conditionally filtering arrays, not generating random numbers in a certain range. (... that might come in another exercise in that same course :D).

 

If you would actually look at the code, you'll see that the array to be inspected does not contain any of the raw generated random numbers, but pairwise differences between random numbers, so a valid match is actually a relatively rare event.

 


LabVIEW Champion. It all comes together in GCentral GCentral
What does "Engineering Redefined" mean??
0 Kudos
Message 8 of 9
(1,495 Views)

@altenbach wrote:

@RavensFan wrote:

Or generate a random number between 0.3 and 1 !


I suspect you are just trying to be funny here 😄

 

Since you did not add any smileys, I'll make a serious comment.

 

Well, the gist of the exercise here is supposed to be conditionally filtering arrays, not generating random numbers in a certain range. (... that might come in another exercise in that same course :D).

 

If you would actually look at the code, you'll see that the array to be inspected does not contain any of the raw generated random numbers, but pairwise differences between random numbers, so a valid match is actually a relatively rare event.

 


Actually I was serious.  But that was because I lost track of the original intent of the thread about conditional filtering and was focused on what the thread evolved to which was the "generate a random number part".

Looking back at the VI in the original message, the intent of the VI was lost among all the wires.  So I'm not really sure what the end goal was.  If the point was to come up with 20 random numbers where the difference between pairs were all greater than/equal to 0.3, otherwise do nothing and try again, the in reality, coming up with a 10 individual random numbers between 0.3 and 1 still applies.  The difference between 2 random numbers is still a random number.  The original pairs of random numbers, as you said, weren't being used anyway other than to generate a difference, and a visual display on the screen.

0 Kudos
Message 9 of 9
(1,488 Views)