LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

divide array by iteration index

I have a for loop inside of a for loop.  The inside loop generates a 1d array, then sends it to a shift register on the outside loop.  The next iteration, it generates another array, then computes a running average of the previous array(s) as:
[(most recent array)/(i+1)] +[i*(previous averaged array from shift register)/(i+1)]
 
Unfortunately, wiring the iteration number (or 1+i) to the divide function seems to screw up polymorphism, any suggestions?  Jonathan
0 Kudos
Message 1 of 8
(3,882 Views)
It should work OK. When you say it "seems to screw up polymorphism," exactly what happens? Do you get some kind of type coercion or errors?

You can convert the iteration index to Double before the calculation if that would help.

Lynn
0 Kudos
Message 2 of 8
(3,878 Views)

Lynn,

     I was wrong about "screwing up polymorphism".  It seems that I was dividing by 0 on the first iteration,because an increment function hadn't wired (just looked wired on top of the wire).  Now, however, I am getting null arrays from the shift register.  I initialize the register, but aways get a null array.  What gives with that? Jonathan

0 Kudos
Message 3 of 8
(3,865 Views)
Actually, it looks like my problem is the initialization of the shift register.  How do I initialize a shift register with an constant array of 0's (of variable length, according to the user's inputs).  Thanks for your help.  Jonathan
0 Kudos
Message 4 of 8
(3,863 Views)
I found the initialize array function.  Smiley Surprised I thought that I could just right click on the shift register, create a constant, and set it to zero, but I guess that didn't work. 
0 Kudos
Message 5 of 8
(3,855 Views)
The initialize array function is probably best. You can create a constant and get what you want. When you create an array constant at the input to a shift register, it is an empty array. Then increase the index value to the last index of the array you want and enter a zero in the element shown. All the elements with lower index values will be filled with the default value for the array (which is zero for numeric data types). If you want it filled with non-zero values, initialize is much better. If you want values which are not all the same, then you need to manually or programmatically create the array with the values you want.

I thought about a divide by zero error, but your algorithm called for i+1 so you should have been OK. Hidden wires are a source of much frustration. It is worth the slight extra effort to avoid them.

Lynn
0 Kudos
Message 6 of 8
(3,797 Views)

One additional small tip for your "cumulative mean" calculation.  You can rewrite your calculation to do just 1 divide operation instead of 2.  It may not matter much unless your arrays are approach a Meg or more, but is something to keep in mind for a rainy day...

rewrite as:  [ (most recent array) + i*(previous averaged array from shift register) ] / (i+1)

-Kevin P.

ALERT! LabVIEW's subscription-only policy came to an end (finally!). Unfortunately, pricing favors the captured and committed over new adopters -- so tread carefully.
0 Kudos
Message 7 of 8
(3,785 Views)


@Kevin Price wrote:
rewrite as:  [ (most recent array) + i*(previous averaged array from shift register) ] / (i+1)

Or even simpler, accumulate the summed array and just divide but i+1. If you are using floating point numbers, it is NOT worth to accumulate the averaged array, because you cannot run out of bits. (Even with integer arrays, you should just add, because the divisions will bias your averaging. Just make sure you are using a representation with sufficient number of bits. If your data is I16, accumulate in a I32 or I64, depending on the total number of averages you are trying to do).

averaged array=(most revent array +sum of all previous arrays)/(i+1)

Here's a quick example (LabVIEW 8.0).


Message Edited by altenbach on 10-19-2007 08:03 AM

Download All
0 Kudos
Message 8 of 8
(3,779 Views)