LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

For loop to get average

How can I use a for loop to get average of set of values?
For example, if I have samples going into the for loop of ten iterations,and end of each ten iterartions I need to calculate the average.
Thanks.
0 Kudos
Message 1 of 28
(5,438 Views)
Hi,
 
Average (arithm) = sum of the elements / number of elements. Do that in the loop. What is the problem? 
Use  a shift register to pass the data from the previous iteration and a build array to append the data.
you'll get the average with the Mean.vi (Analyse=>Math=>Prob and Stats). Be aware that using the build array
function will use much more memory than initialising the shift register and using replace array subset.
But there, you'd need to calculate the number of elements and use array subset before averaging.
 
Dai
Dai

LV 7.1 - WIN XP - RT - FP
0 Kudos
Message 2 of 28
(5,429 Views)

Hi,

There are several ways to do an average.

There is a function in analyse - mathematics - probability and statistics - mean.vi that averages an array of values.

You could also add all the 10 values and then divide by 10.

Take a look at the attached vi,

Hope it helps,

Paulo 

Message 3 of 28
(5,427 Views)
Hello,

one possible way is also:
sum up all elements with "Add Array Elements" function and divide by "array size". This way you dont need a for loop. But using "Mean.vi" or "Std Deviation and variance" from the probability palette is still more elegant...


Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 4 of 28
(5,419 Views)
Thanks for the replies.
Please explain me how to get the average of 100 results samples of attached vi and display them in the text box out side the loop or collect them into an array. I need to store a value for each 100 samples.
Thanks.
0 Kudos
Message 5 of 28
(5,413 Views)
Message 6 of 28
(5,303 Views)
Hello Ravi,

maybe this will help you.
Remember that "Average of 100 elements" and "mean" are no 'text boxes' but numeric indicators...

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 7 of 28
(5,279 Views)

Gerd,

I tried to display the values in a table as in the attached file. Do you have any idea why I get a value for only the first one?There should be a value for each sapling point before averaging.

Next thing is, my original signal is from a while loop, where each iteration of the while loop, I can send a block of samples (say 100) to this vi. So, I need to get series of average values (one each for each 100 samples) and save them in another array.

Can you help me please?

Thanks,

Ravi.

0 Kudos
Message 8 of 28
(5,261 Views)
Hello Ravi,

this should help 🙂


Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 9 of 28
(5,248 Views)

Hi Gerd,

That helped me lot! I learned so much from your answers.

Thanks a lot again!

Ravi.

0 Kudos
Message 10 of 28
(5,227 Views)