LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

remove negative values from array

Solved!
Go to solution

I have a 1D array of data which varies betwen positive and negative values. I am trying to search it for negative values and remove them from the array.  This corresponds to column data in the second 2D array which also needs to be removed. One minor issue is that there is one extra column in the 2D array which has to be skipped over.  I have code which should do this.  The problem is that if I have two negative numbers in a row the code doesn't thow out the second one.   

 

This first picutre is the answer I get when positive and negative values alternate.  It is displaying the correct answer.

posval.PNG

When I change the first1 from positive to a -1 as shown below...

backarray.PNG

... I get the following answer. 

twoneg.PNG

FYI some of the other things such as the conversion are there because the actual data set I am trying to manipulate is huge.  I am testing everything I do with numbers I can actually track before I implement it in my actual program.  That is why I am doing some extra things. 

 

Thanks for the help!

0 Kudos
Message 1 of 7
(5,470 Views)

Your problem is that you are using the "i" in the loop.  "i" is not in sync with your array when deleting something in it.

0 Kudos
Message 2 of 7
(5,453 Views)
Solution
Accepted by topic author Cseaman

Here's the solution, only increase the index when you don't delete an element. In addition you were using the "less then or equal" when you should use the "less than".

 

solution.png

Message 3 of 7
(5,441 Views)

Also, you are taking i+1 for your 2D array, but only i for your 1-D array.  So you are not even working on the same index of the array like you thing you are.

 

I see you have LabVIEW 2011.  I don't think that version had conditional tunnels yet.  If it does, then that is the easiest way to code this.

 

If not, you will need to maintain your own count within a shift register instead of using i.  If you need to delete something, then just past the i through, (because everything later in the array will move up).  If you don't, then add 1 to the counter and put that back into the shift register.

 

Also consider working from the end of the array and move to the front.  Then your index of interest will be Array Length - 1 - the current i value.  Because you will have already worked through the later elements of the array, you won't care that they move up as you delete items from the array.

0 Kudos
Message 4 of 7
(5,439 Views)

Thanks for the help everyone.  I figured it was something simple that I was just overlooking.

0 Kudos
Message 5 of 7
(5,432 Views)

I like using the build array function, when I am trying to eliminate an element from a current array. Something like this (I only used the string example)

 

 

diagram.png

Message 6 of 7
(5,417 Views)

Use auto indexing on the input and conditional indexing on the output.

Can't get much simplier.

 

Your 2D array has more cols than your 1D string array. What are your rules for when that happens

Delete the extra cols?

Pad your 1D string array with extra elements to match the number of cols of your 2D array?

RemoveNegativeValue.png

Omar
Message 7 of 7
(5,400 Views)