Calculate average of floats array using LINQ

Viewed 60

Beginner here,

I have a list that stores arrays of floats (each array reflects a vector, if it matters):

List<float[]>

My goal is to calculate the average values of all these arrays (that is, the vector that contains the average of each element), and store it in an another array of floats.

It's my understanding that using LINQ for my use case would be a good practice. I've given it a try, followed the relevant documentation, and I'm having some trouble.

I would really appreciate seeing what such LINQ query should look like.

1 Answers

You can do it with a help of Linq:

List<float[]> source = ...

var result = source
  .Select(array => array.Average()) // average of each element (array)
  .ToArray();                       // materialize as an array of floats
Related