2.2.3.9.65 matrixbase::SetSizeSetSize
Description
Set the size of any matrixbase derived object.
Syntax
BOOL SetSize( UINT wNumRows, UINT wNumCols, BOOL bKeepData = FALSE )
Parameters
- wNumRows
- [input] The number of rows in the newly sized matrix
- wNumCols
- [input] The number of columns in the newly sized matrix
- bKeepData
- [input] TRUE, when matrix already has data, the data in same position will be same as before doing SetSize.
- If the new size is bigger than original size, for new cells, they are filled with zeros, just same as in new matrix creation;
- FALSE when matrix already has data, the data in same position will be not same as before doing SetSize.
Return
Returns TRUE on success or FALSE on failure.
Examples
EX1
// Simple size setting for a new matrix
void matrixbase_SetSize_ex1()
{
BOOL rc;
matrix mat1;
int wNumRows = 3;
int wNumCols = 4;
rc=mat1.SetSize( wNumRows, wNumCols ); // Set the number of rows and columns in matrix
if(!rc)
printf("Error: SetSize on a matrix failed.\n");
else
{
MatrixPage MatPg1;
MatPg1.Create("Origin");
MatrixLayer MatLy1 = MatPg1.Layers(0);
Matrix Mat1(MatLy1);
Mat1 = mat1;
printf("Observe if the created Origin matrix window %s is %dx%d\n",
Mat1.GetName(),wNumRows,wNumCols);
}
}
EX2
// Shrink an already-existing matrix
void matrixbase_SetSize_ex2()
{
BOOL 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 %s is 3x4.\n",Mat1.GetName());
matrix mat2(mat1);
rc=mat2.SetSize( 2, 2, TRUE); // Shrink matrix to 2x2; bKeepData = TRUE;
if(!rc)
printf("Error2: SetSize on a matrix failed.\n");
else
{
MatrixPage MatPg2;
MatPg2.Create("Origin");
MatrixLayer MatLy2 = MatPg2.Layers(0);
Matrix Mat2(MatLy2);
Mat2 = mat2;
printf("Observe that the shrunk matrix %s is 2x2, and the values are preserved.\n",
Mat2.GetName());
}
}
EX3
// Expand an already-existing matrix
void matrixbase_SetSize_ex3()
{
BOOL 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 %s is 3x4.\n",Mat1.GetName());
matrix mat3(mat1);
rc=mat3.SetSize( 4, 5, FALSE); // Expanded matrix to 4x5; bKeepData = FALSE;
if(!rc)
printf("Error3: SetSize on a matrix failed.\n");
else
{
MatrixPage MatPg3;
MatPg3.Create("Origin");
MatrixLayer MatLy3 = MatPg3.Layers(0);
Matrix Mat3(MatLy3);
Mat3 = mat3;
printf("Observe that the expanded matrix %s is 4x5, and the original data may be broken.\n",
Mat3.GetName());
}
}
Remark
Set the size (number of columns and number of rows) in any matrixbase derived object (e.g. matrix, Matrix).
See Also
matrixbase::GetNumRows, matrixbase::GetNumCols
Header to Include
origin.h
|