05-18-2009 03:25 PM
Hi,
I have a 1D array (say size 1000, each array element varies from 0 to 255) and trying to compare every element to a threshold (say 100). If the array element is less than the threshold of 100 replace it by the value 1, Else replace it by the value 0.
How can I implement this in an efficent way on Labview?
Thanks!
Mayrig
Solved! Go to Solution.
05-18-2009 03:42 PM
Wire your 1D array to a For Loop. Auto indexing enabled. Take each element and compare. Use select function and If match true select 0 to pass out if false select original element. Indexing enabled on output on For Loop will build your new 1D array.
05-18-2009 05:00 PM - edited 05-18-2009 05:01 PM
Hi mayrig,
you can reach your goal without FOR loop:
use "less than 100" with the 1d array, then use "boolean to number". Finished...
(On big arrays this will not be "efficient", but it uses little diagram space )
05-18-2009 05:04 PM - edited 05-18-2009 05:06 PM
mayrig wrote:How can I implement this in an efficent way on Labview?
Define "efficient".
As to the solution shown, here's some simplifications:
EDIT: Written up while Gerd made his response, so point already made.
05-18-2009 05:17 PM
Thanks, this helped me better understand how to operate on array.
I have to perform this operation on a matrix 20* per second, for 2 min, so I meant by efficent somethings that would not compromize my computational speed.
Is there a way to modify your "simplified even more" solution to output either 0 or 255?
Thanks again!
05-18-2009 05:21 PM - edited 05-18-2009 05:24 PM
Yes. Multiply the array by 255. The Multiply operator is polymorphic, like almost all numeric operators, so it can accept an array for one input or both.
Of course, this assumes that you want a new array. You could operate on the original array "in-place" which can be done more efficiently if you have LabVIEW 8.5 or above. (Just waiting for altenbach to chime in to say this. )
05-18-2009 05:23 PM
Perfect!
I'm done for today, but thanks a lot for all your feedback.
05-19-2009 01:27 AM
05-19-2009 09:05 AM
05-20-2009 08:18 AM - edited 05-20-2009 08:18 AM
Unfortunately, simple solutions in LabVIEW often turn out to be inefficient. This is the case here. The original solution produces one data copy. The simplified solution effectively adds another, because the data type changes from U8 to I16. The more simplified version adds yet another data copy due to the boolean array output of the greater than node. You can see all this using the buffer viewer (Tools>>Profile>>Show Buffer Allocations...). However, you can do this with no data copies (other than a couple of local scalar transfer buffers) using the inplace structure and a While loop. Here is an example (VI is LabVIEW 8.5.1). I turned on the buffer viewer before grabbing the image. The black squares show data copies.
If you want more tips on efficiently dealing with arrays in LabVIEW, check out Managing Large Data Sets in LabVIEW.