How Can I Calculate the Mean of a 2D Array? - National Instruments

HomeSupportHow Can I Calculate the Mean of a 2D Array?

How Can I Calculate the Mean of a 2D Array?

Updated Jul 2, 2020

Reported In

Reported In shows products that are verified to work for the solution described in this article. This solution might also apply to other similar products or applications.

Software

LabVIEW

Issue Details

I would like to calculate the mean of all elements in a 2D array. I tried to use Mean.vi, but it seems to only accepts a 1D array as its input. Is there a function that would do the same for a 2D array?

Solution

Unfortunately, there is not a function that is designed to calculate the mean of a whole 2D array by itself. Therefore, you would need to come up with your own algorithm for that. Below is one way of doing it.

Calculate the sum the 2D array by using Add Array Elements.vi

Calculate the size of the 2D array by using Array Size.vi, which returns the number of elements of each dimension in 1D array

Calculate the total number of elements in the 2D by multiplying the elements, calculated at Step 2., by using Multiply Array Elements.vi

Divide the output of Add Array Elements.vi by that of Multiply Array Elements.vi

This technique can extend to 3D arrays and greater dimensions.

Other Support Options

Ask the NI Community

Collaborate with other users in our discussion forums