matrixbase::GetAsVector

Description

Get the values of an entire matrix and assign them to a vector.

Syntax

BOOL GetAsVector( vectorbase & vb, BOOL bByRow = TRUE )

Parameters

vb
[output] Vector used to get the matrix values
bByRow
[input] Use Row order if TRUE otherwise use Column order

Return

Returns TRUE on success and FALSE on failure.

Examples

EX1

void matrixbase_GetAsVector_ex1()
{
    matrix<double> mat1 = {
        {1,  1,  1,  1},
        {2,  4,  6,  8},
        {3,  6,  9, 12}
    };
        vector vR;
        int rc = mat1.GetAsVector(vR);
        if(!rc) 
        printf("Error: GetAsVector failed. rc=%d\n", rc); 
    else{
        for(int ii = 0; ii < vR.GetSize(); ii++) 
                printf("\t%g",vR[ii]);
    }
}

EX2

// Get a vector from a matrix
void matrixbase_GetAsVector_ex2()
{
    matrix<double> mat1 = {
        {1,  1,  1,  1},
        {2,  4,  6,  8},
        {3,  6,  9, 12}
    };
// Sample output of this sample code:
//   The input matrix is mat1.
//   The gotten vector in row-wise:
//      1    1    1    1    2    4    6    8    3    6    9    12
//   The gotten vector in column-wise:
//      1    2    3    1    4    6    1    6    9    1    8    12
//
    MatrixPage MatPg1;
    MatPg1.Create("Origin");
    MatrixLayer MatLy1 = MatPg1.Layers(0);
    Matrix Mat1(MatLy1);
    Mat1 = mat1;
    printf("  The input matrix is %s.\n",Mat1.GetName());
 
    vector vR, vC;
    int ii;
 
    int rc=Mat1.GetAsVector(vR); // Get matrix data in row-wise(default) into vR,
    if(!rc) 
        printf("  Error: GetAsVector failed.\n");
    else
    {
        printf("  The gotten vector in row-wise:\n");
        for(ii = 0; ii < vR.GetSize(); ii++) printf("\t%g",vR[ii]);
        printf("\n");
    }
 
    rc=Mat1.GetAsVector(vC, FALSE); // Get matrix data in column-wise into vC
    if(!rc) 
        printf("  Error: GetAsVector failed.\n");
    else
    {
        printf("  The gotten vector in column-wise:\n");
        for(ii = 0; ii < vC.GetSize(); ii++) printf("\t%g",vC[ii]);
        printf("\n");
    }
}

Remark

Get the values of an entire matrix and assign them to a vector. If the matrix and vector do not have the number of elements then the vector is dynamically resized.

See Also

matrixbase::SetByVector, matrixbase::GetRow, matrixbase::SetRow, matrixbase::GetColumn, matrixbase::SetColumn

header to Include

origin.h