/// <summary> /// Sorts the matrix rows into ascending order, according to the <i>natural ordering</i> of the matrix values in the given column. /// The returned view is backed by this matrix, so changes in the returned view are reflected in this matrix, and vice-versa. /// To sort ranges use sub-ranging viewsd To sort columns by rows, use dice viewsd To sort descending, use flip views ... /// <p> /// <b>Example:</b> /// <table border="1" cellspacing="0"> /// <tr nowrap> /// <td valign="top"><i>4 x 2 matrix: <br> /// 7, 6<br> /// 5, 4<br> /// 3, 2<br> /// 1, 0 <br> /// </i></td> /// <td align="left" valign="top"> /// <p><i>column = 0;<br> /// view = quickSort(matrix,column);<br> /// Console.WriteLine(view); </i><i><br> /// ==> </i></p> /// </td> /// <td valign="top"> /// <p><i>4 x 2 matrix:<br> /// 1, 0<br> /// 3, 2<br> /// 5, 4<br> /// 7, 6</i><br> /// The matrix IS NOT SORTED.<br> /// The new VIEW IS SORTED.</p> /// </td> /// </tr> /// </table> /// /// @param matrix the matrix to be sorted. /// @param column the index of the column inducing the order. /// @return a new matrix view having rows sorted by the given column. /// <b>Note that the original matrix is left unaffected.</b> /// @throws IndexOutOfRangeException if <i>column %lt; 0 || column >= matrix.Columns</i>. /// </summary> public ObjectMatrix2D sort(ObjectMatrix2D matrix, int column) { if (column < 0 || column >= matrix.Columns) { throw new IndexOutOfRangeException("column=" + column + ", matrix=" + Formatter.Shape(matrix)); } int[] rowIndexes = new int[matrix.Rows]; // row indexes to reorder instead of matrix itself for (int i = rowIndexes.Length; --i >= 0;) { rowIndexes[i] = i; } ObjectMatrix1D col = matrix.ViewColumn(column); IntComparator comp = new IntComparator((a, b) => { IComparable av = (IComparable)(col[a]); IComparable bv = (IComparable)(col[b]); int r = av.CompareTo(bv); return(r < 0 ? -1 : (r > 0 ? 1 : 0)); }); RunSort(rowIndexes, 0, rowIndexes.Length, comp); // view the matrix according to the reordered row indexes // take all columns in the original order return(matrix.ViewSelection(rowIndexes, null)); }
/// <summary> /// Returns a string representation of the given matrix with axis as well as rows and columns labeled. /// Pass <i>null</i> to one or more parameters to indicate that the corresponding decoration element shall not appear in the string converted matrix. /// </summary> /// <param name="matrix">The matrix to format.</param> /// <param name="rowNames">The headers of all rows (to be put to the left of the matrix).</param> /// <param name="columnNames">The headers of all columns (to be put to above the matrix).</param> /// <param name="rowAxisName">The label of the y-axis.</param> /// <param name="columnAxisName">The label of the x-axis.</param> /// <param name="title">The overall title of the matrix to be formatted.</param> /// <returns>the matrix converted to a string.</returns> public String ToTitleString(ObjectMatrix2D matrix, String[] rowNames, String[] columnNames, String rowAxisName, String columnAxisName, String title) { if (matrix.Size == 0) { return("Empty matrix"); } String oldFormat = this.formatString; this.formatString = LEFT; int rows = matrix.Rows; int columns = matrix.Columns; // determine how many rows and columns are needed int r = 0; int c = 0; r += (columnNames == null ? 0 : 1); c += (rowNames == null ? 0 : 1); c += (rowAxisName == null ? 0 : 1); c += (rowNames != null || rowAxisName != null ? 1 : 0); int height = r + System.Math.Max(rows, rowAxisName == null ? 0 : rowAxisName.Length); int width = c + columns; // make larger matrix holding original matrix and naming strings ObjectMatrix2D titleMatrix = matrix.Like(height, width); // insert original matrix into larger matrix titleMatrix.ViewPart(r, c, rows, columns).Assign(matrix); // insert column axis name in leading row if (r > 0) { titleMatrix.ViewRow(0).ViewPart(c, columns).Assign(columnNames); } // insert row axis name in leading column if (rowAxisName != null) { String[] rowAxisStrings = new String[rowAxisName.Length]; for (int i = rowAxisName.Length; --i >= 0;) { rowAxisStrings[i] = rowAxisName.Substring(i, i + 1); } titleMatrix.ViewColumn(0).ViewPart(r, rowAxisName.Length).Assign(rowAxisStrings); } // insert row names in next leading columns if (rowNames != null) { titleMatrix.ViewColumn(c - 2).ViewPart(r, rows).Assign(rowNames); } // insert vertical "---------" separator line in next leading column if (c > 0) { titleMatrix.ViewColumn(c - 2 + 1).ViewPart(0, rows + r).Assign("|"); } // convert the large matrix to a string Boolean oldPrintShape = this.printShape; this.printShape = false; String str = ToString(titleMatrix); this.printShape = oldPrintShape; // insert horizontal "--------------" separator line StringBuilder total = new StringBuilder(str); if (columnNames != null) { int i = str.IndexOf(rowSeparator); total.Insert(i + 1, Repeat('-', i) + rowSeparator); } else if (columnAxisName != null) { int i = str.IndexOf(rowSeparator); total.Insert(0, Repeat('-', i) + rowSeparator); } // insert line for column axis name if (columnAxisName != null) { int j = 0; if (c > 0) { j = str.IndexOf('|'); } String s = Blanks(j); if (c > 0) { s = s + "| "; } s = s + columnAxisName + "\n"; total.Insert(0, s); } // insert title if (title != null) { total.Insert(0, title + "\n"); } this.formatString = oldFormat; return(total.ToString()); }