ni.com is currently undergoing scheduled maintenance.

Some services may be unavailable at this time. Please contact us for help or try again later.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to convert boolean array to an numeric count array

I am having a array of Boolean consider the following example

 

asd.jpg

My goal is to count the first LOW or HIGH values of array continuously and it should output the counted value . Then the next value is HIGH or LOW, it should count and display it to next. 

For easy understand I can say diagrammatically which is given below

 

wasasdad.jpg

 

HIGH  value count should be in positive and LOW value count should be in negative. The output is array. I think this is quite simple but I don't know the logic.

0 Kudos
Message 1 of 25
(4,786 Views)

That was a fun little exercise.  What I did was use a Feedback Node to look for a transition.  If they were the same, I incremented a count.  If they were different, I took the count and negated if the previous value was a false and then added it to an array.  For the setup, I also added the inverse of the last value to the array to make sure there was a final transition to get the end of the array.



There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 2 of 25
(4,781 Views)

It was fun.

Here is what I came up with.  The XOR with 1 cycle input delay finds the edges.

The conditional tunnel builds the array on the edges.

Since there is no last edge, I concantenated the counter after the loop finishes.

 

 

 

count_bools.png

0 Kudos
Message 3 of 25
(4,763 Views)

I thought I could do better but I don't think it is.  But there is room for improvement.Boolean to Numbers.png

0 Kudos
Message 4 of 25
(4,754 Views)

I'm a little surprised how tedious this task it but here is my attempt using array search.  It will be interesting to see a side by side comparison of the different methods. 

 

Example_VI_BD.png

aputman
0 Kudos
Message 5 of 25
(4,739 Views)

Oops....i just realized my code is wrong if the last boolean is a F.  The count is not negated.

aputman
0 Kudos
Message 6 of 25
(4,730 Views)

@aputman wrote:

I'm a little surprised how tedious this task it but here is my attempt using array search.  It will be interesting to see a side by side comparison of the different methods. 

 



@aputman wrote:

Oops....i just realized my code is wrong if the last boolean is a F.  The count is not negated.


It is tedious.  I had that same bug.

I think it is fixed now, although it is starting to feel a bit rube-ish.

 

count_bools.pngcount_bools2.png

 

This has to be a more elegant solution to the adding last counter problem... (creating the last edge)

0 Kudos
Message 7 of 25
(4,720 Views)

Okay got it fixed.

I was stuck on using the negate function foolishly.

 

count_bools.png

0 Kudos
Message 8 of 25
(4,697 Views)

@WillD wrote:

I was stuck on using the negate function foolishly.

 


I hear you.  Here is my updated version without negate.

Example_VI_BD.png

aputman
0 Kudos
Message 9 of 25
(4,687 Views)

@aputman wrote:

@WillD wrote:

I was stuck on using the negate function foolishly.

 


I hear you.  Here is my updated version without negate.

 


Hope you don't mind but I found orders of magnitude improvement by removing the for loop, and replacing the while loop with a for loop.  In my test running the code 10000000 times took about 8459ms to execute, but after these two changes took 2ms.  I tried a few other ideas but couldn't come close to this.  Oh automatic error handling and debugging was turned off for the test.

0 Kudos
Message 10 of 25
(4,672 Views)