Accord.Math.MatrixFormatter.ParseJagged C# (CSharp) Méthode

ParseJagged() public static méthode

Converts a matrix represented in a System.String into a jagged array.
public static ParseJagged ( string str, IMatrixFormatProvider provider ) : double[][]
str string
provider IMatrixFormatProvider
Résultat 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 string[] { provider.ParseRowDelimiter }, StringSplitOptions.RemoveEmptyEntries);
            List<double[]> 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 string[] { provider.ParseColDelimiter }, StringSplitOptions.RemoveEmptyEntries);
                List<double> 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.InnerProvider));
                }

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

            return rows.ToArray();
        }

Usage Example

Exemple #1
0
 /// <summary>
 ///   Converts the string representation of a vector to its
 ///   double-precision floating-point number vector equivalent.
 /// </summary>
 /// <param name="str">The string representation of the vector.</param>
 /// <param name="provider">
 ///   The format provider to use in the conversion. Default is to use
 ///   <see cref="DefaultMatrixFormatProvider.CurrentCulture"/>.
 /// </param>
 /// <returns>A double-precision floating-point number matrix parsed
 /// from the given string using the given format provider.</returns>
 ///
 public static double[] Parse(string str, IMatrixFormatProvider provider)
 {
     return(MatrixFormatter.ParseJagged(str, provider).Flatten());
 }
All Usage Examples Of Accord.Math.MatrixFormatter::ParseJagged