Accord.Math.Formats.MatrixFormatter.ParseJagged C# (CSharp) Method

ParseJagged() public static method

Converts a matrix represented in a System.String into a jagged array.
public static ParseJagged ( string str, IMatrixFormatProvider provider ) : double[][]
str string
provider IMatrixFormatProvider
return double[][]
        public static double[][] ParseJagged(string str, IMatrixFormatProvider provider)
        {
            // remove excess spaces
            str = Regex.Replace(str, @" +", " ");

            // First remove starting and trailing tokens
            str = str.Remove(0, provider.ParseMatrixStart.Length);
            str = str.Remove(str.Length - provider.ParseMatrixEnd.Length, provider.ParseMatrixEnd.Length);

            // Now split rows
            string[] strRows = str.Split(new[] {provider.ParseRowDelimiter}, StringSplitOptions.RemoveEmptyEntries);
            var rows = new List<double[]>();

            foreach (string strRow in strRows)
            {
                string row = strRow.Trim();

                // Remove starting and trailing tokens
                if (row.StartsWith(provider.ParseRowStart, StringComparison.Ordinal))
                    row = row.Remove(0, provider.ParseRowStart.Length);
                if (row.EndsWith(provider.ParseRowEnd, StringComparison.Ordinal))
                    row = row.Remove(row.Length - provider.ParseRowEnd.Length, provider.ParseRowEnd.Length);

                // Now split rows values
                string[] strCols = row.Split(new[] {provider.ParseColDelimiter}, StringSplitOptions.RemoveEmptyEntries);
                var values = new List<double>();

                foreach (string strCol in strCols)
                {
                    string col = Regex.Replace(strCol, @"\s", String.Empty);

                    // Remove starting and trailing tokens
                    if (col.StartsWith(provider.ParseColStart, StringComparison.Ordinal))
                        col = col.Remove(0, provider.ParseColStart.Length);
                    if (col.EndsWith(provider.ParseColEnd, StringComparison.Ordinal))
                        col = col.Remove(col.Length - provider.ParseColEnd.Length, provider.ParseColEnd.Length);

                    // finally, parse the value and store
                    values.Add(Double.Parse(col, provider.CultureInfo));
                }

                rows.Add(values.ToArray());
            }

            return rows.ToArray();
        }