MATLAB Answers

Same number of Rows and Cols of a matrix (N*N)

3 views (last 30 days)
David Hastana
David Hastana on 21 Feb 2020
Commented: Rik on 21 Feb 2020
Hello,
I would like to make a program that requires a matrix A to be square (n*n)
for exmple :
[a,b]=size(A)
if a ~= b
input('Error')
end
Is there a matlab function for this ? without using [a,b]=size(A) ?
Thank you in advance

  1 Comment

Rik
Rik on 21 Feb 2020
Backup of question:
Title:
Same number of Rows and Cols of a matrix (N*N)
Body:
Hello,
I would like to make a program that requires a matrix A to be square (n*n)
for exmple :
[a,b]=size(A)
if a ~= b
input('Error')
end
Is there a matlab function for this ? without using [a,b]=size(A) ?
Thank you in advance

Sign in to comment.

Answers (2)

Stephen Cobeldick
Stephen Cobeldick on 21 Feb 2020
Edited: Stephen Cobeldick on 21 Feb 2020
This will return false for non-square matrices, and also if ndims>2:
isequal(0,diff(size(M)))
It is easy to use with assert:
assert(isequal(0,diff(size(M))),'Input must be a square matrix')

  0 Comments

Sign in to comment.


Matt J
Matt J on 21 Feb 2020
validateattributes(A,"numeric",{"square"})

  0 Comments

Sign in to comment.

Sign in to answer this question.