Reputation: 1550
I am doing a MonteCarlo Simulation and am nearly finished writing the code for that part of my application. Ihave the following list:
List<List<double>> myFullList = new List<List<double>>();
List<double> myInnerList = new List<double>();
for(int i = 1; i <= numberOfLoops; i++)
{
for(int i = 1; i <= 10; i++)
{
// Populate inner list with random numbers
myInnerList.Add(double myRandomNumber);
}
// Add the inner list to the full list
myFullList.Add(myInnerList);
}
The list should look something like this:
myFullList[0] = {rand#1,rand#2,rand#3,...,rand#10}
myFulllist[1] = {rand#1,rand#2,rand#3,...,rand#10}
.
.
.
.
myFulllist[1] = {rand#1,rand#2,rand#3,...,rand#10}
I need to average that data to form ONE single list that looks something like this:
List<double> averagedData = new List<double>();
averagedData = {avg#1, avg#2........avg#10}
This output variable will average the data for the same "row" of data in the inner list.
Simple example:
innerList[0] = {1,2,3};
innerList[1] = {3,2,1};
fullList = {innerList[0], innerList[1]};
averageList = {2,2,2};
Upvotes: 2
Views: 570
Reputation: 564433
If you are just trying to average each inner list, you can use:
List<double> averagedData = myFullList.Select(l => l.Average()).ToList();
If you are trying to average the "columns" of data, which seems to be the case from your sample, you could do something like:
var averagedData = myFullList[0].Select((v,c) =>
myFullList.Average(r => r[c])
).ToList();
Upvotes: 5
Reputation: 14477
var averagedData = myFullList.Select(x => x.Average()).ToList();
Upvotes: -2