I want to simplify the generation of a List in C# using LINQ. My goal is to populate the new List with operations using values of another List. I'm willing to use 3rd party libraries, like Deedle or MathNet, if they can reproduce similar performance to my current solution.
A equivalent way to achieve my goal in Matlab would be using simple matrix operations and a dot operation as shown in the following code:
dailyRetList = (dailyCloseList(2:end) - dailyCloseList(1:end-1))./dailyCloseList(1:end-1)
Which creates a new array iterating over dailyCloseList
and for each element it subtracts dailyCloseList[i-1]
from dailyCloseList[i]
, then divide the result by dailyCloseList[i-1]
and finally push the value to the newly created array.
My current solution to tackle the problem is:
var dailyCloseList = new List<double>{11.8d, 11.7d, 13d, 12.6d, 15d};
var dailyRetList = new List<double>();
for (var i = 1; i < dailyCloseList.Count; i++)
{
dailyRetList.Add((dailyCloseList[i] - dailyCloseList[i-1])/dailyCloseList[i-1]);
}