public static MathLib.Matrix ToMatrix(this DataTable me, int firstCol, int lastCol, double nullDataValue) { if (lastCol >= me.Columns.Count) lastCol = me.Columns.Count - 1; int width = lastCol - firstCol + 1; int height = me.Rows.Count; MathLib.Matrix mat = new MathLib.Matrix(height, width); DataRow dr; mat.ColumnNames = Enumerable.Repeat("", width).ToList(); for (int j = 0, c = firstCol; j < width; j++, c++) mat.ColumnNames[j] = me.Columns[c].ColumnName; for (int i = 0; i < height; i++) { dr = me.Rows[i]; for (int j = 0, c = firstCol; j < width; j++, c++) mat[i, j] = dr[c] == DBNull.Value ? nullDataValue : Convert.ToDouble(dr[c]); } return mat; }
public static MathLib.Matrix ToMatrix(this DataView me, int firstCol, int lastCol, double nullDataValue) { if (lastCol >= me.Table.Columns.Count) lastCol = me.Table.Columns.Count - 1; int width = lastCol - firstCol + 1; int height = me.Count; MathLib.Matrix mat = new MathLib.Matrix(height, width); DataRowView dr; for (int i = 0; i < height; i++) { dr = me[i]; for (int j = 0, c = firstCol; j < width; j++, c++) mat[i, j] = dr[c] == DBNull.Value ? nullDataValue : Convert.ToDouble(dr[c]); } return mat; }