2.2.3.9.54 matrixbase::ReorderRowWise

Description

rearrange the matrix in row-wise according to the given indices

Syntax

BOOL ReorderRowWise( const vector<UINT> & vnIndices, int nC1 = 0, int nC2 = -1 )

Parameters

vnIndices
[input] array or row index to reorder a column
nC1
[input] 1st Column index to reorder
nC2
[input] last Column index to reorder, use -1 to indicate the last column in matrix

Return

Returns TRUE on successful exit or FALSE on failure.

Examples

EX1

// Reordering a matrix based on the given order vector
void matrixbase_ReorderRowWise_ex1()
{
    BOOL rc;
    
    // The last column of each row in the matrix has the sum of the cells in the row
    matrix mat1 = {
        {1, 2, 6,  9},  
        {2, 5, 4, 11},
        {3, 7, 2, 12},
        {4, 0, 4,  8},
        {5, 2, 3, 10},
        {6, 1, 0,  7}
    };
    
    // This vector contains the order to make the matrix sorted by the last column
    vector<UINT> vOrders = {5, 3, 0, 4, 1, 2};

//    Reordered matrix sorted by the last column (indicated by vOrders) is:
//        {6, 1, 0,  7}
//        {4, 0, 4,  8}
//        {1, 2, 6,  9}
//        {5, 2, 3, 10}
//        {2, 5, 4, 11}
//        {3, 7. 2, 12}

    MatrixPage MatPg1;
    MatPg1.Create("Origin");
    MatrixLayer MatLy1 = MatPg1.Layers(0);
    Matrix Mat1(MatLy1);
    Mat1 = mat1;
    printf("  Original matrix is %s. The last element of each row presents a sum of the cells in the row.\n",Mat1.GetName());
    
    MatrixPage MatPg2;
    MatPg2.Create("Origin");
    MatrixLayer MatLy2 = MatPg2.Layers(0);
    Matrix Mat2(MatLy2);
    Mat2 = mat1;
    
    rc=Mat2.ReorderRowWise(vOrders); //Reordering rows based on the vOrders vector
    if(!rc) 
        printf("  Error: ReorderRowWise failed.\n");
    else
        printf("  Reordered matrix sorted by the last column values is %s.\n",Mat2.GetName());
}

Remark

See Also

matrixbase::ReorderColWise

Header to Include

origin.h