cancel
Showing results for 
Search instead for 
Did you mean: 

Search and Erase Element from Array

Search and Erase Element from Array

Message contains an attachment

I have an 1D array of N (approx 90K) elements. I am trying to get rid of all zeros and NaN in the array. What is the fastest way to do so? My solution has been search for array, delete element, repeat the process until the search comes up to be empty (index value =-1).

 

As always, your insights/tricks/inputs are much appreciated.

 

Best,


Santiago

Message Edited by FightOnSCTrojan on 08-15-2008 11:47 AM
22 REPLIES 22
altenbach
Knight of NI

Re: Search and Erase Element from Array

Message contains a hyperlink

You never want to use "delete from array" in a loop, because every resizing operation forces a new copy of the array in memory. You should do everything "in place" and trim the excess at the end.

 

Do something like the bottom code in this VI. Simply adapt it to your requirements.

 

(Original discussion)

 

Please let us know if you have any questions.

~jiggawax~
Trusted Enthusiast

Re: Search and Erase Element from Array

What about NaN, +Inf and -Inf?  I think if you can type it in a numeric control you should also be able to type it in the Selector Label on the case statement. 
jigg
CTA, CLA
testeract.com
~Will work for kudos and/or BBQ~
altenbach
Knight of NI

Re: Search and Erase Element from Array

Message contains an image Message contains an attachment

~jiggawax~ wrote:
What about NaN, +Inf and -Inf?  I think if you can type it in a numeric control you should also be able to type it in the Selector Label on the case statement. 

No, case selectors are integer, so NaN, Inf, etc. have no meaning.

 

Here's a quick example on what I would do. Be careful comparing NaN, for example of you ask NaN=NaN, the answer is false. You need to use the special primitive from the comparison palette.

 

Of course, since you are dealing with DBL, only true zeroes will get removed, so all caveats comparing floating point numbers still apply. If this is a concern, you need to deal with it with some extra code.

 

Message Edited by altenbach on 08-15-2008 12:41 PM

Re: Search and Erase Element from Array

Altenbach....which tool/function did you use last on your example (outside the for loop, before connecting it to the indicator)?

Sorry...I am running LV 8.2...so I could not open the VI

Re: Search and Erase Element from Array

Nevermind....answer my own question...reshape array
Highlighted

Re: Search and Erase Element from Array

Message contains a hyperlink

Altenbach:

 

As always…Thanks for your suggestion.

I tried to do some time study contrasting your proposed method and my original method.

The result….Altenbach (2ms), Santiago (6806ms).

Therefore he is 3000+ times better than me.

Rightfully so,

 

Santiago

 

p.s. Would you mind giving some thoughts about http://forums.ni.com/ni/board/message?board.id=170&thread.id=349480? Thanks!

 

altenbach
Knight of NI

Re: Search and Erase Element from Array

Message contains a hyperlink

FightOnSCTrojan wrote:

The result….Altenbach (2ms), Santiago (6806ms).


Sounds about right. Your original method is only good if you need to remove very few elements (such as zero or one Smiley Very Happy).


FightOnSCTrojan wrote:

p.s. Would you mind giving some thoughts about http://forums.ni.com/ni/board/message?board.id=170&thread.id=349480? Thanks!


 

 I would go with Karissa's "numeric" method if it is appropriate for your data. Going to strings and back is expensive.

 

Message Edited by altenbach on 08-15-2008 02:57 PM
sticyfinger
Member

Re: Search and Erase Element from Array

Message contains an attachment

Can someone please tell me how to delete the zeros from an array. but only the beginning zeros and ending zeros. since i am gettting waveform data some zero in the middle of array should not be deleted. 

altenbach
Knight of NI

Re: Search and Erase Element from Array

(Please start a new thread instead of adding to a 2 year old discussions that is not very similar. Also please attach a real VI containing some default data instead of an image.)

 

Simply find the first and last nonzero element and then take the array subset of all data in-between.

 

ALso note that comparisons of DBL data with zero might cause problems.