matrixbase::SetByVector

Description

Set the values of a matrix equal to the values of a vector.

Syntax

int SetByVector( vectorbase & vb, BOOL bByRow = TRUE )

Parameters

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

Return

0 if successful and the number of elements in the vector is the same as the number of elements in the matrix, otherwise the number of elements in the matrix minus the number of elements in the vector is returned.

Examples

EX1

// Set values into a matrix from a vector
void matrixbase_SetByVector_ex1()
{
    int rc;
    
    matrix mat1;
    rc=mat1.SetSize(2,3); // #rows=2, #cols=3
    MatrixPage MatPg1;
    MatPg1.Create("Origin");
    MatrixLayer MatLy1 = MatPg1.Layers(0);
    Matrix Mat1(MatLy1);
    Mat1 = mat1;
    // initial matrix is:
    //    {0,  0,  0}
    //    {0,  0,  0}

    vector vR = {1, 1, 1, 2, 2, 2};
    rc = Mat1.SetByVector(vR); // set the data row-wise (default)
    if(rc!=0) 
        printf("  Warning: Given vector size differs from the matrix size of %s. Size difference=%d\n",
          Mat1.GetName(),rc);
    else
        printf("  Data in vector {1 1 1 2 2 2} have been inserted in row order into %s.\n",
          Mat1.GetName());
    // Result matrix is:
    //    {1,  1,  1}
    //    {2,  2,  2}

    MatrixPage MatPg2;
    MatPg2.Create("Origin");
    MatrixLayer MatLy2 = MatPg2.Layers(0);
    Matrix Mat2(MatLy2);
    Mat2 = mat1;
    
    vector vC = {1, 1, 2, 2, 3, 3};
    rc = Mat2.SetByVector(vC, FALSE); // set the data column-wise
    if(rc!=0) 
        printf("  Warning: Given vector size differs from the matrix size of %s. Size difference=%d\n",
          Mat2.GetName(),rc);
    else
        printf("  Data in vector {1 1 2 2 3 3} have been inserted in cloumn order into %s.\n",
          Mat2.GetName());
    // Result matrix is:
    //    {1,  2,  3}
    //    {1,  2,  3}
}

EX2

// Set a vector value in a column - Error case
void matrixbase_SetByVector_ex2()
{
    int rc;
    
    matrix mat1;
    rc=mat1.SetSize(2,3); // #rows=2, #cols=3
    MatrixPage MatPg1;
    MatPg1.Create("Origin");
    MatrixLayer MatLy1 = MatPg1.Layers(0);
    Matrix Mat1(MatLy1);
    Mat1 = mat1;
    // initial matrix is:
    //    {0,  0,  0}
    //    {0,  0,  0}

    vector vR = {1, 2, 3, 4};
    rc = Mat1.SetByVector(vR); // set the data row-wise (default)

    if(rc>0)
        printf("  Warning: Given vector {1, 2, 3, 4} is smaller than %s. Size difference=%d\n",
          Mat1.GetName(),rc);
    else if(rc<0)
        printf("  Warning: Given vector {1, 2, 3, 4} is larger than %s. Size difference=%d\n",
          Mat1.GetName(),rc);
    else
    printf("  The result matrix %s has been correctly set.\n",
          Mat1.GetName());
}

Remark

Set the values of a matrix equal to the values of a vector. If the matrix has more elements than the vector then vector values are assigned until they are exausted and the remaining matrix values are left untouched. If the vector has more elements than the matrix then vector values are assigned to the last element of the matrix and the remaining elements of the vector are ignored.

See Also

matrixbase::GetAsVector, matrixbase::SetRow, matrixbase::GetRow, matrixbase::SetColumn, matrixbase::GetColumn

header to Include

origin.h