LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Efficent Array Manipulation

Solved!
Go to solution

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

0 Kudos
Message 1 of 11
(4,952 Views)
Solution
Accepted by topic author mayrig

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.

 

 

Brian
Message 2 of 11
(4,940 Views)

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 Smiley Wink )

Message Edited by GerdW on 05-19-2009 12:01 AM
Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 3 of 11
(4,919 Views)

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.

 

Message Edited by smercurio_fc on 05-18-2009 05:06 PM
Message 4 of 11
(4,914 Views)

 

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!

0 Kudos
Message 5 of 11
(4,902 Views)

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. Smiley Very Happy)

Message Edited by smercurio_fc on 05-18-2009 05:24 PM
Message 6 of 11
(4,897 Views)

Perfect!

 

I'm done for today, but thanks a lot for all your feedback.

0 Kudos
Message 7 of 11
(4,892 Views)

HI smercurio,

 

also can't wait for Altenbach:

The multiplication will be in-place anyway, if the wire isn't branching before and no datatype changes are involved!

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 8 of 11
(4,853 Views)
Sorry, should have been more clear. I mean in terms of in-place with the original array - i.e., replacing the elements in the original array. The poster did initially mention this, but it does not appear (based on the response) as if that was a real requirement, and may have been a mistake in terminology.
0 Kudos
Message 9 of 11
(4,808 Views)

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.

Message Edited by DFGray on 05-20-2009 08:18 AM
Download All
Message 10 of 11
(4,745 Views)