From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, 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: 

Add to new values to an existing 1D array in a while loop

Solved!
Go to solution

Hello,

I have a while loop that collects data from a DAQ Assistant and stores it into a 1D array. The number of loops is set by the user. I am trying to average the data from each iteration of the loop. So far I am only able to get the array to overwrite the previous array values with the new ones.

if Number of loops = 3

First Iteration                      Second Iteration                         Third Iteration                           New 1D Array        [Divide by Loop #

4.97                                      4.99                                               4.98                                            14.94                       for the average]
4.97                                      4.99                                               4.98                                            14.94
4.96                                      4.98                                               4.97                                            14.91
4.98                                      5.00                                               4.99                                            14.97
4.97                                      4.99                                               4.98                                            14.94

I would like to make this as general as possible as the loop number will vary depending on the user input. 

 

0 Kudos
Message 1 of 16
(6,096 Views)

please post VI

0 Kudos
Message 2 of 16
(6,083 Views)

LoopSnippet.png

 

Notice that the tool to sum an entire array is built into labview. Divide by the array length and you have the average.

Study labview array behavior and auto indexing for powerful ways to do common array tasks. For example, if you wire an array to the multiply icon, the output is a new array containing the product of EVERY element of the array and the multiplier. No need to use a loop at all!

 

Message 3 of 16
(6,081 Views)

If I understand you correcty, you could use a shift register to store your final array.  Then just sum each time you calculate an array.  Lastly take the average by dividing by the size (user specified for the for loop).

 

This is a rough example of what I mean:

 

Example_VI2.png

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If someone helped you out, please select their post as the solution and/or give them Kudos!
Message 4 of 16
(6,074 Views)

I like the creative solution you have provided however, I am trying to keep the average at each element so that I can graph the average at each point. Therefore, I need the average to be a 1D array of the same initial length, not a scalar. 

I am trying to avoid using shift registers.

0 Kudos
Message 5 of 16
(6,072 Views)

@sgtpppr685 wrote:

I like the creative solution you have provided however, I am trying to keep the average at each element so that I can graph the average at each point. Therefore, I need the average to be a 1D array of the same initial length, not a scalar. 

I am trying to avoid using shift registers.


1. Do you mean you want the average after each iteration as well?  This can be done pretty easily by moving the division inside my loop.  Perhaps I am misunderstanding you though.

 

2. Why avoid shift registers?  They are amazing and effective.  They are an essential LabVIEW tool imo 😛  Plus they work wonderfully for your current problem.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If someone helped you out, please select their post as the solution and/or give them Kudos!
Message 6 of 16
(6,068 Views)

I will post my VI, just let me clean it up a bit. Shift Registers are not my friend today.

0 Kudos
Message 7 of 16
(6,061 Views)

You absolutely should be using shift registers here.  How else is iteration 2 going to know what values existed during iteration 1????

If you add two arrays (just the basic math operation) they add by index (index 2 + index 2) then just divide by the number of loops after the for loop has finished.

 

If you want to calculate the moving average (update average during the loops) you'll need to employ a moving average algorithm... NOTE that simply dividing by the iteration number here will give you the WRONG RESULTS.

Message 8 of 16
(6,054 Views)

@pjr1121 wrote:

If you want to calculate the moving average (update average during the loops) you'll need to employ a moving average algorithm... NOTE that simply dividing by the iteration number here will give you the WRONG RESULTS.


Good catch!  I stand corrected on that part 😛  Its been a while since I worried about means.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If someone helped you out, please select their post as the solution and/or give them Kudos!
0 Kudos
Message 9 of 16
(6,051 Views)

I am using Labview 2011

I have tried using shift registers and because I exit the loop, a feedback node appears and it seems to be nullifying the array instead of storing the data. Perhaps I just don't understand how to properlyy use a feedback node.

Thank you for all the help so far. I've really been pulling my hair out.

0 Kudos
Message 10 of 16
(6,048 Views)