matrixbase::SetRow

Description

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

Syntax

int SetRow( vectorbase & vb, UINT wIndexRow )

Parameters

vb
[input] Vector whose values are assigned to the row
wIndexRow
[input] Specifies the 0 based offset or row index of the row in the matrix

Return

Returns 0 if successful and the vector size matches the matrix row size, otherwise returns the size of the matrix row minus the size of the vector.

Examples

EX1

// Set a vector value in a Row
void matrixbase_SetRow_ex1()
{
    int rc;
    
    matrix<double> mat1 = {
        {1,  1,  1,  1},
        {2,  4,  6,  8},
        {3,  6,  9, 12}
    };
    MatrixPage MatPg1;
    MatPg1.Create("Origin");
    MatrixLayer MatLy1 = MatPg1.Layers(0);
    Matrix Mat1(MatLy1);
    Mat1 = mat1;
    printf("  The original matrix is %s.\n",Mat1.GetName());
    
    matrix mat2(mat1);    
    MatrixPage MatPg2;
    MatPg2.Create("Origin");
    MatrixLayer MatLy2 = MatPg2.Layers(0);
    Matrix Mat2(MatLy2);
    Mat2 = mat2;
    vector v2 = {0, 0, 0, 0};
    rc=Mat2.SetRow(v2, 1);  // Set 2nd Row(as the index starts 0) of Mat2
    if(rc!=0) 
        printf("  Warning: Given vector size differs from the row size of %s. Size difference=%d\n",
          Mat2.GetName(),rc);
    else
    printf("  The 2nd row in the result matrix %s has been correctly changed.\n",
          Mat2.GetName());
}

EX2

// Set a vector value in a row - Error case
void matrixbase_SetRow_ex2()
{
    int rc;
    
    matrix<double> mat1 = {
        {1,  1,  1,  1},
        {2,  4,  6,  8},
        {3,  6,  9, 12}
    };
    MatrixPage MatPg1;
    MatPg1.Create("Origin");
    MatrixLayer MatLy1 = MatPg1.Layers(0);
    Matrix Mat1(MatLy1);
    Mat1 = mat1;
    printf("  The original matrix is %s.\n",Mat1.GetName());
              
    matrix mat3(mat1);    
    MatrixPage MatPg3;
    MatPg3.Create("Origin");
    MatrixLayer MatLy3 = MatPg3.Layers(0);
    Matrix Mat3(MatLy3);
    Mat3 = mat3;
    vector v3 = {0, 0, 0, 0, 0};
    rc=Mat3.SetRow(v3, 1);  // Set 2nd row(as the index starts 0) of Mat3
    if(rc>0)
        printf("  Warning: Given vector is shorter than the row of %s. Size difference=%d\n",
          Mat3.GetName(),rc);
    else if(rc<0)
        printf("  Warning: Given vector is longer than the row of %s. Size difference=%d\n",
          Mat3.GetName(),rc);
    else
    printf("  The 2nd row in the result matrix %s has been correctly changed.\n",
          Mat3.GetName());
}

Remark

Set the values of a matrix row equal to the values of a vector. The row is specified by a 0 based row index. If the matrix row and vector do not have the same size then the maximum number of elements possible are assigned from the vector to the matrix row.

See Also

matrixbase::GetRow, matrixbase::SetColumn, matrixbase::GetColumn, matrixbase::GetAsVector, matrixbase::SetByVector

header to Include

origin.h