2 でインデックス付けされた行だけでなく、すべての行に対してこのループをループする必要があります。また、二重解析でエラーが発生します。
以下を行う効率的な方法はありますか?
ファイルには可変長の行を含めることができます。したがって、各行のサイズを格納する配列を維持する必要があります。
public static void ReadFile()
{
int lineNo;
List<List<double>> numbers = new List<List<double>>();
foreach (string line in File.ReadAllLines("Data.txt"))
{
var list = new List<float>();
foreach (string s in line.Split(new[]{',', ' '}, StringSplitOptions.RemoveEmptyEntries))
{
int i;
if(double.TryParse(s, out i))
{
list.Add(i);
lineNo++;
}
}
numbers.Add(list);
}
var specialNumber = numbers[3][4]; // gives line 3 number 4
var specialLine = numbers[2].ToArray(); // gives an array of numbers of line 2
double[] rowTotal;
double[] squareRowTotal;
double[] rowMean;
//I need to loop this loop for all rows and not just the row indexed by 2. Also I am getting an error in the double parsing.
for (int j=0; j<(specialLine.Length); j++)
{
rowTotal[2] = rowTotal[2] + numbers[2][j];
squareRowTotal[2] = squareRowTotal[2] + numbers[2][j] * numbers[2][j];
}
for (int k = 0; k < lineNo; k++)
{
rowMean[k] = rowTotal[k] / numbers[k].Length;
}
}