How to check if an array is a square matrix in Matlab and Octave
To check if an array is a square matrix in Matlab and Octave, use the function issquare(M)
issquare(M)
The M parameter is an array
The function returns in output
- 1 if the array M is a square matrix
- 0 if the array M is not a square matrix
What is a square matrix? A matrix is called square matrix if it has the same number of rows and columns.
Examples
Example 1
Define an array
>> M=[1 2;3 4]
M =
1 2
3 4
Use the issquare() function to determine if the array is a square matrix
>> issquare(M)
ans = 1
The output result is 1.
The array is a square matrix.
Example 2
Define an array
>> M=[1 2 3;4 5 6]
M =
1 2 3
4 5 6
Calculate if the matrix is square using the issquare() function
>> issquare(M)
ans = 0
The output result is 0.
The array is not a square matrix.