LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Calculate array mean in a recursive way

Solved!
Go to solution

@developer001 wrote:

Is there a simpler way to do this?


Your code is pure LabVIEW gibberish. Your inner FOR loop has absolutely no purpose.

 

Do you need to keep a seperate mean for each row, or do you just want the global mean of the 2D array?

 

To just keep the global sum and mean of a 2D array, here's what you could do.

0 Kudos
Message 11 of 23
(1,476 Views)

Well, your VI doesn't do the same as mine. Did you actually mean that you wanted to record the average of all 30 data points? I thought you needed the separate averages of the three sets of 10.

 

To get that, just take the mean of your new 2D array and add it to the previous result. Since the initial arrays are all the same size, it doesn't matter whether you take the mean first or add everything together first.

 

Or you could do the same thing I did, but do it with 2-D arrays, then calculate your mean of the second dimension. The commutative property (or is it the distributive property? Whatever.) of math operations is a wonderful thing (of course, I don't mean all math operations).

 

Then to get the overall average, just take the mean of your resulting 3-cell array.

 

(BTW, if you straighten out your wires and try not to go backwards, your VIs will be a lot easier to read.)

 

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 12 of 23
(1,475 Views)

@altenbach wrote:

@developer001 wrote:

Is there a simpler way to do this?


Your code is pure LabVIEW gibberish. Your inner FOR loop has absolutely no purpose.

 



I guess he didn't want it to be random over x, but x^2*10 ? I just used his "random generator" because it made no difference in what I was doing. I thought he wanted to see what was happening each step of the way to that global average.

 

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 13 of 23
(1,470 Views)

Yes I need to keep a separate mean for each row, as they are data for different channels.

0 Kudos
Message 14 of 23
(1,469 Views)

Ok I think I need to clear up exactly what I want to do:

Each 3x10 array is a data block.

Each row is a different channel (3 channels), so I need to take the mean of each row separately. So the mean array would be 3x1.

 

 

I will be receiving 3x10 blocks of data continously, and I will need to take the mean contiously.

 

Sorry guys if I confused you! Smiley Happy

0 Kudos
Message 15 of 23
(1,456 Views)
Solution
Accepted by topic author developer001

Then do this...

 

 

Download All
0 Kudos
Message 16 of 23
(1,455 Views)

Hey, alterbach, I think your solution is identical to mine, although I did not put the indexing into a for loop (now attached). In G, is there any difference in performance between wiring the function expressed for each index and using a for loop? I don't know any of the internals here.

 

Running_Mean_of_Arrays_Using_For_Loop.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 17 of 23
(1,434 Views)

Thanks guys it works just the way it should!! Smiley Very Happy

0 Kudos
Message 18 of 23
(1,430 Views)

@camerond wrote:

Hey, alterbach, I think your solution is identical to mine, although I did not put the indexing into a for loop (now attached). In G, is there any difference in performance between wiring the function expressed for each index and using a for loop? I don't know any of the internals here.


You should always use autoindexing. Most likely there is no performance difference and the compiler will generate identical code. However, of you don't use autoidenxing, the diagram is more cluttered and there are more places for bugs to hide or for mistakes to occur. What if you wire to the other index by accident? Your code is also broken, because N is not wired.

 

Your code is not the same, because it is less scalable. For example, if there is a need to change the number of rows, you need to make changes in several places (initiaize array, generate random array), and if you miss one place, the thing will not work right. In my code, only a single change needs to be made. Much easier to keep things consistent.

 

My code automatically adapts to 2D arrays of any size.

0 Kudos
Message 19 of 23
(1,418 Views)

Whoops, I got it. Didn't realize that I had lost the autoindexing when I modified the code from his requirement of a fixed 3 x X array to what should have been a general solution. Just popped that out and didn't look back, didn't even notice it broke the arrow. Funny, because I always look back. Oh, well, I guess Friday the 13th struck me after all.

 

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 20 of 23
(1,408 Views)