LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Adjacent array values

Solved!
Go to solution

What would be the most efficient way in labview to search a 1d array of 0's and 1's, and store the indices of adjacent 1's as a new row in a 2d array? For example say I have an array of 0 0 0 0 1 1 1 0 1 1 0, the resulting 2d array would look like this:

 

4 5 6

8 9

 

where these numbers represent the indexes of the adjacent 1's.

 

Any help with this is appreciated.

0 Kudos
Message 1 of 8
(4,270 Views)

What have you tried?  A for loop with a couple shift registers should be all you need.

 

What will go into the space after the 9 in your 2-D array?  2-D arrays are rectangular meaning every row needs the same number of columns, and every column needs the same number of rows.

 

Is this homework?

0 Kudos
Message 2 of 8
(4,265 Views)
Part of thesis work, so homework I guess. Here is the .vi I have created previously, however it is quite inefficient, doesn't present the data in rows, and gets stuck in a continuous loop if the value searched for is 0.
0 Kudos
Message 3 of 8
(4,261 Views)
Solution
Accepted by topic author eword

A couple problems.

 

Change your Count and Row counts from Double representation, to I32 or some other integer type.  That is probably why you are getting stuck in a loop searching for zero.  Round off errors in doubles can lead to inequalities between numbers that you might otherwise consider the same.

 

You have localitis.  You have a race condition for Row count in the True case of the case structure.  Do you want the Row count incremented by 1, or read to index the 2-D array first?  As it is now, there is no control over which happens first.  Eliminate all the local variables.  Replace them with wires and start using shift registers to maintain the values between loop iterations.

 

Another problem is your while loop stops when your count is equal to the number of elements in the array.   Your count starts out as zero.  It appears that the count is incremented on each iteration of the loop.  Eventually your count will equal the number of elements in the array and the loop will stop.  But all other code in that iteration will still have to occur.  If you have a 5 element array, your indices go from 0 to 4.  But your count is now 5.  The loop will stop when the iteration completes, but now you are trying to index out element 5 (which doesn't exist) and also element 6 (5 +1) which also doesn't exist.  Actually, you'll have the problem in the previous iteration because element 5 (4+1) doesn't exist.

 

Another problem is the way you use insert into array.  If you start out with an empty array, and try to insert into an array in any other row or column besides 0, you get nothing.  Or if you have 4 rows and try to insert into row 5 or higher, nothing gets added.  Insert into array is the wrong function to use probably 95% of the time.  You should be using Build Array.

 

Why do you have a case structure that in the False case puts out a True, and in the True case puts out a False?  That is a Rube Goldberg.  It won't cause a problem, but it is just unnecessary code.  Just wire the comparison out to the stop and put a NOT operator on it.  If you change the loop condition to Continue if True, then you can eliminate the Not! as well.  Also, instead of adding a 1 to a number, there is a +1 increment operator in the numeric palette.  (A little less code means a little cleaner diagram.)  Also, you don't need two separate Index Array functions.  You can use one and resize it downwards.  If you wire the Count into the upper one, the lower one (that you just added by expanding the function) will give you the next index after count.)

 

I don't have your global variable.  (Where is that data coming from? Another VI?)  If you can replace that global with an array constant with some typical data, it would give us something to work with.

Message 4 of 8
(4,247 Views)

Ok I have tried to follow your instructions as well as I can, however I still have a continuous loop and I can't seem to implement Build Array. Here are the changes I have made. Also I have added some sample data into a control.

0 Kudos
Message 5 of 8
(4,238 Views)
After a little more thought I finally got it working. Thanks a bundle.
0 Kudos
Message 6 of 8
(4,223 Views)

LVStudent wrote:
After a little more thought I finally got it working. Thanks a bundle.

 

 
I would recommend to add a few more thoughts! 🙂 You are doing this way too complicated!
 
  • First of all, why are "count" and "row count" controls?? If the user would change them, the results would be wrong. Since they should always start at zero, they should be diagram constants instead.
  • Since the outer loop needs to go exactly once over each element of the input array in ascending order, use an autoindexing FOR loop. No logic needed. A while loop only needs to be used if you cannot determine at the start of the loop how many iterations will be needed.
  • As has been mentioned already, a 2D array is unsuitable for an output because you will have rows of variable length. Why not use an array of clusters, where each cluster contains a 1D array?
  • You can wire numerics directly to case structures, again no logic operations needed.
 
Here's a quick draft (LV 8.0) how it could be done (2 versions, one with the 2D array and one with the array of clusters. Pick one!). Modify as needed. There are plenty of ways to do all this. Maybe you can simplify it even more. 😉 
 
 
Message 7 of 8
(4,202 Views)
Great. Thanks a lot for your help. :robothappy:
0 Kudos
Message 8 of 8
(4,179 Views)