hungcuiga1
hungcuiga1

Reputation: 118

Average of System.Collections.Generic.List<(float[],float[])>

I have a list of tuples of arrays of floats - List<(float[], float[])>. I want to calculate the average of all the floats in all the arrays in all the tuples in the list.

That is, how do I average all of floats contained in this list?

Upvotes: 0

Views: 330

Answers (1)

Scott Hannen
Scott Hannen

Reputation: 29242

This method will take a List<(float[], float[])> and return the average of all the values in all the arrays in all the tuples in the list:

private float AverageAllValuesInListOfTuplesOfArraysOfFloats(List<(float[], float[])> input)
{
    // Create a new list which will contain the "flattened" list of 
    // float values.
    var allValues = new List<float>();

    // Iterate over all the tuples of arrays
    foreach ((float[], float[]) tupleOfArrays in input)
    {
        // in each tuple of arrays, add the items from both
        // arrays the the flattened list which contains all
        // the values.
        allValues.AddRange(tupleOfArrays.Item1);
        allValues.AddRange(tupleOfArrays.Item2);
    }

    // Now we've got one big list of values. Average it.
    return allValues.Average();
}

Upvotes: 1

Related Questions