LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

initializing array

For a 1-dim array x(i) (i=0..N-1), I need to compute the quantity
S=sum(i=0..N-1){i*x(i)}.   
I am trying to do it by creating an array y(i), where y(i)=i.  Then I can compute z(i)=x(i)*y(i) and sum the values of z(i).  However, I can't even create y(i).  Can you tell me how to create y(i) or a way to compute S without creating y(i)?  It would be easy with C or Matlab, but I can't seem to get Labview's for loop to do what I want.
 
My attempt to create y(i)=i (i=0..N-1):
I tried creating an N-elem. array outside a for loop, wiring it to the loop (with auto-indexing enabled), & using replace_array_subset.vi inside the loop to stick the right values in the right locations.  But I get a broken wire error inside the loop: "you're trying to connect a source that is scalar to a sink that is array".  It seems that on the "inside" of the tunnel thru the for loop, only a scalar value gets thru the tunnel, even though an array is wired to the tunnel. 
I also tried creating an empty array, passing that to a foor loop, and using insert_into_array.vi to sequentially add elements, but got similar error.
 
Thanks,
 
Newbie
0 Kudos
Message 1 of 5
(3,237 Views)
Unless I'm misunderstanding your post, this should be easily accomplished with the LabVIEW code in the attached screenshot.  As you can see, we're multiplying each element of the array by the iteration count of the For Loop, building those values up at the array output border, then summing them once the loop is done. 
 
It would probably be more efficient performance-wise to use a shift register and keep a running sum within the For Loop, but I figured we'd start off with the simple solution.
 
Hope this helps,
-D

Message Edited by Darren on 01-10-2006 12:56 PM

Message 2 of 5
(3,223 Views)

HI,

 

Use a shift register to hold the "running sum".  Use the "i" from the for loop for i [what you were calling y(i)].  Wire the array x(i) into the for loop.  If you right click the tunnel that is created when you wire the x(i) array to the loop edge you will see Enabale Indexing (which will be chaecked if you wire to a for loop, and unchecked if you wire to a while loop).  If indexing is enabled, LV will grab one point from the array each iteration, and when theend of the array happens, the for loop will autostop (no need to wire to the big blue N).

 

I attached a screen shot of the diagram.
 
Let me know if you have any problems.
 
J
Message 3 of 5
(3,209 Views)
Feed your X array into a FOR loop. Inside the FOR loop. The loop will autoindex, meaning that at the first iteration you get the first element, etc. The loop will run for each elements in X. Inside the FOR loop, multiply the current element with the iteration terminal and feed the result outside the loop on the right. Autoindexing at the loop boundary will automatically create your array to be summed. Feed it through a "add array ements" node.
 
You should probably do a few tutorials first.
 
Message 4 of 5
(3,204 Views)

Thank you very much to all who replied.  Your recommendations were just what I needed, allowing me to compute the "mean frequency" of a power spectrum:

                 sum(i=0..N-1) { i*P(i) }

mean Freq = df * ------------------------

                  sum(i=0..N-1){ P(i)}

where df = interval in Hz between each element of the power spectrum array.
Thanks again.
0 Kudos
Message 5 of 5
(3,185 Views)