LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Calculate array mean in a recursive way

Solved!
Go to solution

Hi

I need a way to calculate the mean of continuous 3x10 array without doing the calculation on all the values every single time.

 

I have blocks of 3x10 array as input(coming in continuosly). First round, t1, I want to take the mean of each row, so 3x1 array called "current mean". This will be inserted into a 2d array called "all means".

The next iteration, t2, I will have another 3x10 block of data coming in. I will take the mean again, and insert it into "all means". Now I want to take the mean of the rows of "all means", so the mean of all the data I currently have, so the mean of t1 and t2. 

This method works, except that since I am constantly inserting into array, the size increases and slows down everything.

Ideally, I want to have only 2 columns of data in the "all means" array at any given time. The old mean previously calculated, and the new block of data. So I need a way of saving the "old mean", and replacing it with the data in the 2d array.

I attached a very simple doagram of what I have...this would be in a for loop running continuously.

 

0 Kudos
Message 1 of 23
(4,890 Views)

In order to do what you want, you're going to have to multiply (weight) your already-calculated aggregate mean by the number of iterations you have already run, then add them to your current data. That way you only deal with two arrays and a scalar. I'll leave the programming details up to you.

 

But, take out that flat sequence. You don't need it, and it just makes things ugly.

 

Cameron

 

To err is human, but to really foul it up requires a computer.
The optimist believes we are in the best of all possible worlds - the pessimist fears this is true.
Profanity is the one language all programmers know best.
An expert is someone who has made all the possible mistakes.

To learn something about LabVIEW at no extra cost, work the online LabVIEW tutorial(s):

LabVIEW Unit 1 - Getting Started</ a>
Learn to Use LabVIEW with MyDAQ</ a>
0 Kudos
Message 2 of 23
(4,880 Views)

Thanks!

But I dont quite understand why we need to multiply it by the number of iterations we have already run...?

0 Kudos
Message 3 of 23
(4,869 Views)

Well, just take the numbers 1, 2, and 3, then calculate the mean of them with and without weighting the first aggregate mean (and dividing by the total number of data points - 3).

 

Cameron

 

To err is human, but to really foul it up requires a computer.
The optimist believes we are in the best of all possible worlds - the pessimist fears this is true.
Profanity is the one language all programmers know best.
An expert is someone who has made all the possible mistakes.

To learn something about LabVIEW at no extra cost, work the online LabVIEW tutorial(s):

LabVIEW Unit 1 - Getting Started</ a>
Learn to Use LabVIEW with MyDAQ</ a>
0 Kudos
Message 4 of 23
(4,859 Views)

I understand what I need to do now...

but there is just one problem:

I take the mean of the current input, multiply it by the number of iterations, add it to the next set of data and repeat this again.

 

But how do I make it calculate the mean of the new array this time?

 

I've attached my vi...I just cant figure out how to reuse the value of the new array with feedback nodes!

 

Thanks!!

Download All
0 Kudos
Message 5 of 23
(4,823 Views)

Actually, when I wired it up, it's a lot simpler than that. Just a while loop with a shift register. The shift register carries the previous sum (not average) back for you.

 

I dummyed up the input by just using a scalar, but obviously you could feed in an array. If you had a predetermined number of points to handle (as this one actually does), just change the while loop to a for loop.

 

Running_Mean.png

 

No feedback nodes or any of that messy stuff.

 

Cameron

 

To err is human, but to really foul it up requires a computer.
The optimist believes we are in the best of all possible worlds - the pessimist fears this is true.
Profanity is the one language all programmers know best.
An expert is someone who has made all the possible mistakes.

To learn something about LabVIEW at no extra cost, work the online LabVIEW tutorial(s):

LabVIEW Unit 1 - Getting Started</ a>
Learn to Use LabVIEW with MyDAQ</ a>
0 Kudos
Message 6 of 23
(4,800 Views)

What I can see from your program is that it takes the average of the first element of the array, then the average of the first 2 elements, then the first 3 and the first 4.

But I want it to take the average of the entire first array, then the average of the first and second arrays, and so forth. So the entire thing should be in while loop to continously run and get new array values.

 

I tried some modifications but still no luck...thanks a lot for your help!!

0 Kudos
Message 7 of 23
(4,791 Views)

No, I said I'm using a scalar for the example I drew up, and I slowed it way down so I could make sure visually it worked. You don't have to do anything fundamentally different to use an array input.

 

Get your average of the current 3x10 array separately (however you're taking/averaging data now), then use that 3x1 array in the while loop (as a subVI, if you want). If you feed in a 3x1 array, you will get 3x1 arrays at your indicators (if you copy/modify what I've got there, just make sure you have written your incoming array into the VI before you create the indicators, otherwise you might get the dreaded BrokenLines and will just have to delete the indicators and add them again).

 

Cameron

 

To err is human, but to really foul it up requires a computer.
The optimist believes we are in the best of all possible worlds - the pessimist fears this is true.
Profanity is the one language all programmers know best.
An expert is someone who has made all the possible mistakes.

To learn something about LabVIEW at no extra cost, work the online LabVIEW tutorial(s):

LabVIEW Unit 1 - Getting Started</ a>
Learn to Use LabVIEW with MyDAQ</ a>
0 Kudos
Message 8 of 23
(4,772 Views)

Here's what I'm talking about (also attached). All I did was change the input from a scalar to a 3-cell array (a 3x1 array is really a 1-dimensional array) and make sure the indicators were arrays instead of scalars. Just stick your own array-generating VI in where I have the random-number-array-generating for loop.

 

Running_Mean_of_Arrays.png

 

Cameron

 

To err is human, but to really foul it up requires a computer.
The optimist believes we are in the best of all possible worlds - the pessimist fears this is true.
Profanity is the one language all programmers know best.
An expert is someone who has made all the possible mistakes.

To learn something about LabVIEW at no extra cost, work the online LabVIEW tutorial(s):

LabVIEW Unit 1 - Getting Started</ a>
Learn to Use LabVIEW with MyDAQ</ a>
0 Kudos
Message 9 of 23
(4,762 Views)

Thanks its much more clear now.

I modified your code a bit to do exactly what I want now...so it takes the sum of all the elements in the array in order to calculate the mean.

Right now my code works fine for a 1d array input.

However I want to pass it in a 2d array, where each row is basically the 1d array that I can already process.

 

So the first time it takes a 3x10 array and calculates the mean of each row(10 points)

the next time it takes another 3x10 array and caluclates the mean over 20 points.

 

I know its probably a simple modification, and adding an indexed for loop for my input 2d array.

The problem is the result(mean) is a 1 d array, and the new data is a 2d array.

In order to be able to add them they need to have the same dimensions.

One way is to insert the 1d array into a 2d array (duplicating the values in each cell) and then add it.

But I will need to initialize the arrays manually and becomes a mess.

 

Is there a simpler way to do this?

 

Thanks a lottttttt!!Smiley Happy

0 Kudos
Message 10 of 23
(4,738 Views)