Search code examples
c#arraysrowstextinputdatainputstream

How to find the number of each elements in the row and store the mean of each row in another array using C#?


I am using the below code to read data from a text file row by row. I would like to assign each row into an array. I must be able to find the number or rows/arrays and the number of elements on each one of them.

I would also like to do some manipulations on some or all rows and return their values.

I get the number of rows, but is there a way to to loop something like:

    *for ( i=1 to number of rows)
    do
    mean[i]<-row[i]
    done
    return mean*


var data = System.IO.File.ReadAllText("Data.txt");

var arrays = new List<float[]>();

var lines = data.Split(new[] {'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries);

foreach (var line in lines)
{
    var lineArray = new List<float>();

    foreach (var s in line.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries))
    {
        lineArray.Add(Convert.ToSingle(s));
    }
    arrays.Add(lineArray.ToArray());

}

var numberOfRows = lines.Count();
var numberOfValues = arrays.Sum(s => s.Length);

Solution

  • Found an efficient way to do this. Thanks for your input everybody!

    private void ReadFile()
        {
            var lines = File.ReadLines("Data.csv");
            var numbers = new List<List<double>>();
            var separators = new[] { ',', ' ' };
            /*System.Threading.Tasks.*/
            Parallel.ForEach(lines, line =>
            {
                var list = new List<double>();
                foreach (var s in line.Split(separators, StringSplitOptions.RemoveEmptyEntries))
                {
                    double i;
    
                    if (double.TryParse(s, out i))
                    {
                        list.Add(i);
                    }
                }
    
                lock (numbers)
                {
                    numbers.Add(list);
                }
            });
    
            var rowTotal = new double[numbers.Count];
            var rowMean = new double[numbers.Count];
            var totalInRow = new int[numbers.Count()];
    
            for (var row = 0; row < numbers.Count; row++)
            {
                var values = numbers[row].ToArray();
    
                rowTotal[row] = values.Sum();
                rowMean[row] = rowTotal[row] / values.Length;
                totalInRow[row] += values.Length;
            }