An identity matrix is a matrix that has 1s in the diagonal indexes and 0s in all other indexes. Given input matrix mat, check if the matrix is an identity matrix.
| Input |
Expected outputs |
mat = { {1,0,0}, {0,1,0}, {0,0,1} };
|
The matrix is an Identity matrix.
|
mat = { {0,1,0,0}, {0,1,0,0}, {0,0,1,0},};
|
The matrix is *not* an Identity matrix.
|
mat = { {1,0}, {0,1}, {1,0}, {0,1}};
|
The matrix is *not* an Identity matrix.
|
mat = { {0,1,0,0}, {0,1,0,0}, {0,0,1,0}, {0,0,0,2}};
|
The matrix is *not* an Identity matrix.
|
| The java method should accept following input parameters: mat (integer matrix).
|
| Only square matrices can be identity matrices. If number of rows of mat are not equal to number of columns, return false from the program.
|
| Iterate through mat using a for loop, using variable rIdx as loop counter. Loop variable rIdx will be initialized with value 0 and will iterate through all the matrix’s columns one by one till rIdx < mat.length:
Iterate through mat using a for loop, using variable cIdx as loop counter. Loop variable cIdx will be initialized with value 0 and will iterate through all the matrix’s columns one by one till cIdx < mat[0].length:
If rIdx == cIdx, then check if mat[rIdx][cIdx]=1; it is not 1, the matrix is not an identity matrix, return false.
If rIdx != cIdx, then check if mat[rIdx][cIdx]=0; it is not 0, the matrix is not an identity matrix, return false.
|
| If the above loops are completed and the program has not return false, it means the matric is an identity matrix, so return true.
|
Below fully running code can be copied and run on Eclipse or other Java IDEs. Refer the classname in code below. If the class name below is "A", save the code below to a file named A.java before running it.
Be sure to try your own test cases to enhance your understanding !
You can also tweak the code to optimize or add enhancements and custom features.
public class MdArrayIdentityMatrix {
public boolean matArrayIdentityMatrix(int[][] mat) throws Exception{
if (mat.length != mat[0].length) { return false; }
for (int rIdx=0; rIdx < mat.length; rIdx++) {
for (int cIdx=0; cIdx < mat[0].length; cIdx++) {
if (rIdx==cIdx) {
if (mat[rIdx][cIdx] != 1) {
return false;
}
}else {
if (mat[rIdx][cIdx] != 0) {
return false;
}
}
}
}
return true;
}
public static void main(String[] args) {
MdArrayIdentityMatrix ap=new MdArrayIdentityMatrix();
boolean ret;
try {
int[][] arr = { {1,0,0},
{0,1,0},
{0,0,1}
};
print2dArray(arr, "The 2d Array");
ret=ap.matArrayIdentityMatrix(arr);
if (ret) {
System.out.println("\n The matrix is an Identity Matrix");
}else {
System.out.println("\n The matrix is *not* an Identity Matrix");
}
int[][] arr1 = { {0,1,0,0},
{0,1,0,0},
{0,0,1,0},
};
print2dArray(arr1, "The 2d Array");
ret=ap.matArrayIdentityMatrix(arr1);
if (ret) {
System.out.println("\n The matrix is an Identity Matrix");
}else {
System.out.println("\n The matrix is *not* an Identity Matrix");
}
int[][] arr2 = { {1,0},
{0,1},
{1,0},
{0,1}
};
print2dArray(arr2, "The 2d Array");
ret=ap.matArrayIdentityMatrix(arr2);
if (ret) {
System.out.println("\n The matrix is an Identity Matrix");
}else {
System.out.println("\n The matrix is *not* an Identity Matrix");
}
int[][] arr3 = { {0,1,0,0},
{0,1,0,0},
{0,0,1,0},
{0,0,0,2}
};
print2dArray(arr3, "The 2d Array");
ret=ap.matArrayIdentityMatrix(arr3);
if (ret) {
System.out.println("\n The matrix is an Identity Matrix");
}else {
System.out.println("\n The matrix is *not* an Identity Matrix");
}
}catch (Exception exception) {
System.out.print("Exception,"+ exception);
exception.printStackTrace();
}
}
public static void print2dArray(int[][] intArray, String label) throws Exception {
// Case 1: The input Array is null !!
if (intArray == null) { System.out.println("\n\n Input 2d Array was null !! \n"); return; }
// Case 2: Print input Array by index (first to last)
System.out.println();
System.out.println("*********************************************************************");
System.out.print(label+" : \n");
int rIdx=0; int cIdx=0;
for (rIdx=0; rIdx < intArray.length; rIdx++) {
for (cIdx=0; cIdx < intArray[rIdx].length; cIdx++) {
System.out.print(intArray[rIdx][cIdx]);
if (cIdx < intArray[rIdx].length) System.out.print(",");
}
System.out.print("\n");
}
System.out.println();
System.out.println("**********************************************************************");
System.out.println();
Thread.sleep(2000);
return;
}
}