/// <summary> Reads a matrix from a reader. The first line in the file should /// contain the number of rows and columns. Subsequent lines /// contain elements of the matrix. /// /// </summary> /// <param name="r">the reader containing the matrix /// </param> /// <throws> Exception if an error occurs </throws> /// <seealso cref="write(Writer)"> /// </seealso> /// <author> FracPete, taken from old weka.core.Matrix class /// </author> //UPGRADE_ISSUE: Class hierarchy differences between 'java.io.Reader' and 'System.IO.StreamReader' may cause compilation errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1186'" public Matrix(System.IO.StreamReader r) { System.IO.StreamReader lnr = new System.IO.StreamReader(r.BaseStream, r.CurrentEncoding); System.String line; int currentRow = - 1; //UPGRADE_WARNING: Method 'java.io.LineNumberReader.readLine' was converted to 'System.IO.StreamReader.ReadLine' which may throw an exception. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1101'" while ((line = lnr.ReadLine()) != null) { // Comments if (line.StartsWith("%")) continue; Tokenizer st = new Tokenizer(line); // Ignore blank lines if (!st.HasMoreTokens()) continue; if (currentRow < 0) { int rows = System.Int32.Parse(st.NextToken()); if (!st.HasMoreTokens()) { //UPGRADE_ISSUE: Method 'java.io.LineNumberReader.getLineNumber' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javaioLineNumberReadergetLineNumber'" throw new System.Exception("Line " + currentRow + ": expected number of columns"); } int cols = System.Int32.Parse(st.NextToken()); double[][] tmpArray = new double[rows][]; for (int i = 0; i < rows; i++) { tmpArray[i] = new double[cols]; } A = tmpArray; m = rows; n = cols; currentRow++; continue; } else { if (currentRow == RowDimension) { //UPGRADE_ISSUE: Method 'java.io.LineNumberReader.getLineNumber' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javaioLineNumberReadergetLineNumber'" throw new System.Exception("Line " + currentRow + ": too many rows provided"); } for (int i = 0; i < ColumnDimension; i++) { if (!st.HasMoreTokens()) { //UPGRADE_ISSUE: Method 'java.io.LineNumberReader.getLineNumber' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javaioLineNumberReadergetLineNumber'" throw new System.Exception("Line " + currentRow + ": too few matrix elements provided"); } set_Renamed(currentRow, i, System.Double.Parse(st.NextToken())); } currentRow++; } } if (currentRow == - 1) { //UPGRADE_ISSUE: Method 'java.io.LineNumberReader.getLineNumber' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javaioLineNumberReadergetLineNumber'" throw new System.Exception("Line " + currentRow + ": expected number of rows"); } else if (currentRow != RowDimension) { //UPGRADE_ISSUE: Method 'java.io.LineNumberReader.getLineNumber' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javaioLineNumberReadergetLineNumber'" throw new System.Exception("Line " + currentRow + ": too few rows provided"); } }
/// <summary> creates a matrix from the given Matlab string.</summary> /// <param name="matlab"> the matrix in matlab format /// </param> /// <returns> the matrix represented by the given string /// </returns> /// <seealso cref="toMatlab()"> /// </seealso> public static Matrix parseMatlab(System.String matlab) { Tokenizer tokRow; Tokenizer tokCol; int rows; int cols; Matrix result; System.String cells; // get content cells = matlab.Substring(matlab.IndexOf("[") + 1, (matlab.IndexOf("]")) - (matlab.IndexOf("[") + 1)).Trim(); // determine dimenions tokRow = new Tokenizer(cells, ';'); rows = tokRow.Count; tokCol = new Tokenizer(tokRow.NextToken(), ' '); cols = tokCol.Count; // fill matrix result = new Matrix(rows, cols); tokRow = new Tokenizer(cells, ';'); rows = 0; while (tokRow.HasMoreTokens()) { tokCol = new Tokenizer(tokRow.NextToken(), ' '); cols = 0; while (tokCol.HasMoreTokens()) { result.set_Renamed(rows, cols, System.Double.Parse(tokCol.NextToken())); cols++; } rows++; } return result; }