2.2.3.9.46 matrixbase::MakeIdentity

Description

Make this matrix an N x N identity matrix or an M x N rectangular matrix with 1's on the diagonal and zeros elsewhere.

Syntax

int MakeIdentity( int mNumRows, int nNumCols = -1 )

Parameters

mNumRows
[input] Number of rows in the result matrix
nNumCols
[input] Number of columns of the result matrix (default -1 means mNumRows = nNumCols)

Return

Returns 0 on success or an error code on failure:

-1=nNumCols and mNumRows are not positive integers (nNumCols can be -1)

Examples

EX1

// Make an identity matrix
void matrixbase_MakeIdentity_ex1()
{
    matrix mIdent, mIdent2,mIdent3;
 
    MatrixPage MatPg1;
    MatPg1.Create("Origin");
    MatrixLayer MatLy1 = MatPg1.Layers(0);
    Matrix Mat1(MatLy1);
 
    int rc=mIdent.MakeIdentity(3);
    // Create 3X3 identity matrix:
    //   {1 0 0}
    //   {0 1 0}
    //   {0 0 1}
    if(rc!=0) 
        printf("  Error: Making a 3X3 identity matrix failed. Error Code=%d\n",rc);
    else {
        Mat1=mIdent;
        printf("  Created 3X3 identity matrix %s.\n",Mat1.GetName());
    }
 
    MatrixPage MatPg2;
    MatPg2.Create("Origin");
    MatrixLayer MatLy2 = MatPg2.Layers(0);
    Matrix Mat2(MatLy2);
 
    rc=mIdent2.MakeIdentity(3,5);
    // Create 3X5 identity matrix:
    //   {1 0 0 0 0}
    //   {0 1 0 0 0}
    //   {0 0 1 0 0}
    if(rc!=0) 
        printf("  Error: Making a 3X5 identity matrix failed. Error Code=%d\n",rc);
    else {
        Mat2=mIdent2;
        printf("  Created 3X5 identity matrix %s.\n",Mat2.GetName());
    }
    
    MatrixPage MatPg3;
    MatPg3.Create("Origin");
    MatrixLayer MatLy3 = MatPg3.Layers(0);
    Matrix Mat3(MatLy3);
    
     rc=mIdent3.MakeIdentity(3,2);
    // Create 3X2 identity matrix:
    //   {1 0 }
    //   {0 1 }
    //   {0 0 }
    if(rc!=0) 
        printf("  Error: Making a 3X2 identity matrix failed. Error Code=%d\n",rc);
    else {
        Mat3=mIdent3;
        printf("  Created 3X2 identity matrix %s.\n",Mat3.GetName());
    }
}

Remark

See Also

matrixbase::GetLowerTriangular, matrixbase::GetUpperTriangular

Header to Include

origin.h