matrixbase::SetColumn
SetColumn
Description
Set the values of a matrix column equal to the values of a vector.
Syntax
int SetColumn( vectorbase & vb, UINT wIndexCol )
Parameters
- vb
- [input] Vector whose values are assigned to the column
- wIndexCol
- [input] Specifies the 0 based offset or column index of the column in the matrix
Return
Returns 0 if successful and the vector size matches the matrix column size, otherwise returns the size of the matrix column minus the size of the vector.
Examples
EX1
// Set a vector value in a column
void matrixbase_SetColumn_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};
rc=Mat2.SetColumn(v2, 2); // Set 3rd column(as the index starts 0) of Mat2
if(rc!=0)
printf(" Warning: Given vector size differs from the column size of %s. Size difference=%d\n",
Mat2.GetName(),rc);
else
printf(" The 3rd column in the result matrix %s has been correctly changed.\n",
Mat2.GetName());
}
EX2
// Set a vector value in a column - Error case
void matrixbase_SetColumn_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};
rc=Mat3.SetColumn(v3, 2); // Set 3rd column(as the index starts 0) of Mat3
if(rc>0)
printf(" Warning: Given vector is shorter than the column of %s. Size difference=%d\n",
Mat3.GetName(),rc);
else if(rc<0)
printf(" Warning: Given vector is longer than the column of %s. Size difference=%d\n",
Mat3.GetName(),rc);
else
printf(" The 3rd column in the result matrix %s has been correctly changed.\n",
Mat3.GetName());
}
Remark
Set the values of a matrix column equal to the values of a vector. The column is specified by a 0 based column index. If the matrix column and vector do not have the same size then the maximum number of elements possible are assigned from the vector to the matrix column.
See Also
matrixbase::GetColumn, matrixbase::GetRow, matrixbase::SetRow, matrixbase::GetRow, matrixbase::GetAsVector, matrixbase::SetByVector
header to Include
origin.h
|