Discussion:
Search and Erase Element from Array
(too old to reply)
FightOnSCTrojan
2008-08-15 19:10:05 UTC
Permalink
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,
SantiagoMessage Edited by FightOnSCTrojan on 08-15-2008 11:47 AM


Sample.gif:
Loading Image...
altenbach
2008-08-15 19:40:17 UTC
Permalink
You never want to use &quot;delete from array&quot; in a loop, because every resizing operation forces a new copy of the array in memory. You should do everything &quot;in place&quot; and trim the excess at the end.&nbsp;Do&nbsp;something like the bottom code in <a href="http://forums.ni.com/ni/attachments/ni/170/116060/3/RemoveZeroes_1D.vi" target="_blank">this VI</a>. Simply adapt it to your requirements. &nbsp;(<a href="http://forums.ni.com/ni/board/message?board.id=170&amp;view=by_date_ascending&amp;message.id=116060#M116060" target="_blank">Original discussion</a>) &nbsp;Please let us know if you have any questions.
~jiggawax~
2008-08-15 19:40:17 UTC
Permalink
What about NaN, +Inf and -Inf?&nbsp; 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.&nbsp;
altenbach
2008-08-15 19:40:19 UTC
Permalink
~jiggawax~ wrote:
What about NaN, +Inf and -Inf?&nbsp; 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.&nbsp;
No, case selectors are integer, so NaN, Inf, etc. have no meaning.&nbsp;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.&nbsp;Of course, since you are dealing with DBL, oly true zeroes will get removed, so all caveats comparing floating point numbers still apply. If this is a concern, oyu need to deal with it with some extra code.&nbsp;<img src="Loading Image..." border="0" width="610" height="304"> Message Edited by altenbach on 08-15-2008 12:39 PM


removeBadStuff.gif:
http://forums.ni.com/ni/attachments/ni/170/349709/1/removeBadStuff.gif


RemoveBadStuff1D.vi:
http://forums.ni.com/ni/attachments/ni/170/349709/2/RemoveBadStuff1D.vi
FightOnSCTrojan
2008-08-15 21:40:19 UTC
Permalink
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
FightOnSCTrojan
2008-08-15 21:40:20 UTC
Permalink
Nevermind....answer my own question...reshape array
FightOnSCTrojan
2008-08-15 21:40:20 UTC
Permalink
Altenbach: &nbsp;
As always&hellip;Thanks for your suggestion.
I tried to do some time study contrasting your proposed method and my original method.
The result&hellip;.Altenbach (2ms), Santiago (6806ms).
Therefore he is 3000+ times better than me.
Rightfully so,&nbsp;
Santiago
&nbsp;
p.s. Would you mind giving some thoughts about <a href="http://forums.ni.com/ni/board/message?board.id=170&amp;thread.id=349480" target="_blank">http://forums.ni.com/ni/board/message?board.id=170&amp;thread.id=349480</a>? Thanks!
&nbsp;
altenbach
2008-08-15 22:10:06 UTC
Permalink
FightOnSCTrojan wrote:

The result&hellip;.Altenbach (2ms), Santiago (6806ms).
Sounds about right. Your original&nbsp;method is only good if you need to remove very few elements (such as zero or one :D).
FightOnSCTrojan wrote:

p.s. Would you mind giving some thoughts about <a href="http://forums.ni.com/ni/board/message?board.id=170&amp;thread.id=349480" target="_blank">http://forums.ni.com/ni/board/message?board.id=170&amp;thread.id=349480</a>? Thanks!
&nbsp;&nbsp;I would go with Karissa's &quot;numeric&quot; method if it is appropriate for your data. Going to strings and back is expensive.&nbsp;Message Edited by altenbach on 08-15-2008 02:57 PM
Continue reading on narkive:
Loading...