From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Interpolate Gaps in a Large Array

Solved!
Go to solution

I have a very large array of doubles, and some conditions (that boil down to boolean) that determine whether each element should be kept or discarded; the discarded elements to be replaced by interpolated values.  Because the array is very large, I want the code to be very efficient.  Attached is what I have come up with.  Can anyone come up with something more efficient?

"If you weren't supposed to push it, it wouldn't be a button."
0 Kudos
Message 1 of 10
(1,910 Views)

What you have works.  Why do you think it would be inefficient?

 

It looks like you keep one copy of the array and do Replace Array Subset to replace elements.

It looks like you just advance through the array just one time.

 

Both of those things make it seem efficient to me.

 

 

0 Kudos
Message 2 of 10
(1,874 Views)

@RavensFan wrote:

What you have works.  Why do you think it would be inefficient?

 

It looks like you keep one copy of the array and do Replace Array Subset to replace elements.

It looks like you just advance through the array just one time.

 

Both of those things make it seem efficient to me.

 

 


It's not that I thought that it was necessarily inefficient; but often people here find clever ways to make things better.

"If you weren't supposed to push it, it wouldn't be a button."
0 Kudos
Message 3 of 10
(1,841 Views)

If there is a way to make it better, Altenbach will find it!  😄

0 Kudos
Message 4 of 10
(1,828 Views)
Solution
Accepted by topic author paul_cardinale

@RavensFan wrote:

If there is a way to make it better, Altenbach will find it!  😄


Maybe not "better", but certainly faster to program and debug (fewer places of bugs to hide! ;))

 

 

altenbach_0-1595790450571.png

 

(attached VI contains both versions to compare operation)

 

NOTE: the results differ if the last element of the boolean array is FALSE. It is not clear what you would want in that scenario. See post below to get the same result.

 

Message 5 of 10
(1,814 Views)
Solution
Accepted by topic author paul_cardinale

@altenbach wrote:
NOTE: the results differ if the last element of the boolean array is FALSE. It is not clear what you would want in that scenario.

 


If you want the same result even if the boolean array ends in FALSE, this might work.

 

altenbach_0-1595791585901.png

 

altenbach_1-1595791635625.png

 

 

Message 6 of 10
(1,808 Views)

@paul_cardinale wrote:

Because the array is very large, I want the code to be very efficient.  Attached is what I have come up with.  Can anyone come up with something more efficient?


"Very large" is a fuzzy term. Do you have some numbers? The efficiency between the two algorithms also depends on the TRUE probability of the boolean array. It is sparse or mostly true? (This determines the size of the intermediary data structures in my code).

 

One problem in your code is the initial "prepending" of elements, which can potentially lead to new array allocations (there is an allocation dot). Try to avoid that.

 

0 Kudos
Message 7 of 10
(1,804 Views)

Thank you.  That's just what I was looking for.

"If you weren't supposed to push it, it wouldn't be a button."
0 Kudos
Message 8 of 10
(1,733 Views)

@altenbach wrote:

@paul_cardinale wrote:

Because the array is very large, I want the code to be very efficient.  Attached is what I have come up with.  Can anyone come up with something more efficient?


"Very large" is a fuzzy term. Do you have some numbers? The efficiency between the two algorithms also depends on the TRUE probability of the boolean array. It is sparse or mostly true? (This determines the size of the intermediary data structures in my code).

 

One problem in your code is the initial "prepending" of elements, which can potentially lead to new array allocations (there is an allocation dot). Try to avoid that.

 


There will be a few million to several million elements.  The % of TRUE elements in the boolean array is unpredictable, but most likely from 30% - 50%.

I should have realized that, because of the nature of the data source, the beginning elements will always be keepers, so I really don't need prepend anything. Also, the data near the end of the array is of lesser importance (and declining importance as you go farther up the array).  Most of the upper values will be near 0; and what happens with the very last element is inconsequential.

"If you weren't supposed to push it, it wouldn't be a button."
0 Kudos
Message 9 of 10
(1,727 Views)

@paul_cardinale wrote:
I should have realized that, because of the nature of the data source, the beginning elements will always be keepers, so I really don't need prepend anything. Also, the data near the end of the array is of lesser importance (and declining importance as you go farther up the array).  Most of the upper values will be near 0; and what happens with the very last element is inconsequential.

I am not sure where that -1 comes from and if it is important. Maybe it can be left out. If the first element is guaranteed to be valid and the last element asymptotically zero, you could just ensure these while keeping all arrays at the same size.

 

altenbach_0-1595861995287.png

 

Still, I would recommend some benchmarks.

0 Kudos
Message 10 of 10
(1,722 Views)