hello, I'm supposed to check if the matrix is square if not, disp a message. when i used [n,m]=size(A), if n~=m, disp, it seems to work; if i use [n,n]=size(A),

50 views (last 30 days)
can you please tell me what's wrong in below and how to use [n,n]=size(A) properly? silly question but you can tell i'm new. Thansk a lot.
[n,n] = size(A);
disp('coefficient matrix A must be square')

Accepted Answer

Walter Roberson
Walter Roberson on 24 Feb 2022
[n,n] = size(A);
That means to assign the first output of size() to n, and then to assign the second output of size to n, overwriting the value that was just written.
It has the same effect as
[~,n] = size(A);
or as
[ThisVariableIsNotUsed, n] = size(A);
clear ThisVariableIsNotUsed
It does not in any way compare the two outputs of A.
... and you do not have any if statement, so unless the [n,n]=size(A) errors out, the disp() is always going to be run.

More Answers (2)

David Hill
David Hill on 24 Feb 2022
Has to be two different variable; otherwise you are overriding the first assignment. You can use a single variable, such as:
n=size(A);%will be an array
isequal(n(1),n(2))

Christophe
Christophe on 6 Jun 2025
After having defined the matrix A, I'd just type
A' - A
if MATLAB gives an error, it means that A isn't square : )
  3 Comments
Christophe
Christophe on 6 Jun 2025
ok, so try A * A and it gives an error, or eig(A) and it gives an error
btw MATLAB should givre an error with A'-A, since you cannot subtract matrices with different dimensions, but ok
>> A'-A
ans =
0 -1 -2 -3 -4 -5
1 0 -1 -2 -3 -4
2 1 0 -1 -2 -3
3 2 1 0 -1 -2
4 3 2 1 0 -1
5 4 3 2 1 0
>> A*A
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the
first matrix matches the number of rows in the second matrix. To operate on each
element of the matrix individually, use TIMES (.*) for elementwise multiplication.
Related documentation
>> eig(A)
Error using eig
Input matrix must be square.
Steven Lord
Steven Lord on 6 Jun 2025
btw MATLAB should givre an error with A'-A, since you cannot subtract matrices with different dimensions, but ok
That has not been the case for several years. See this documentation page.
To forestall the questions/comments I expect you to post next based on previous feedback:
  • No, this is not a bug.
  • Yes, we know what mathematics says the rules are.
  • This is a generalization of scalar expansion that has been part of MATLAB since Cleve's original Fortran version IIRC.
  • No, there is no way to disable this behavior.
  • We discussed internally whether we should define new operators to perform this expansion operation rather than changing the existing operators before we released it.
  • Yes, I am certain this is not a bug.
To expand upon one of Stephen23's objection to your transpose-and-subtract or your eig approaches to telling if a matrix is square: if the matrix is large and square, and so the check succeeds, how long is it going to take to perform those calculations and how much memory is the resulting array (and any intermediate temporary variables created during the calculations) going to consume? Calling size (I would argue you should also ask ismatrix as well, to safeguard against a 3-d array with the right combinations of sizes) is going to create a few scalar values and it doesn't have to do much (or any) calculations in the process.
The 3-d concern:
A = rand(4, 2, 2);
[rows, colsSortOf] = size(A)
rows = 4
colsSortOf = 4
issquareSortOf = isequal(rows, colsSortOf)
issquareSortOf = logical
1
This is documented behavior of the size function, BTW. From that documentation page:
  • When dim is not specified and fewer than ndims(A) output arguments are listed, then all remaining dimension lengths are collapsed into the last argument in the list. For example, if A is a 3-D array with size [3 4 5], then [sz1,sz2] = size(A) returns sz1 = 3 and sz2 = 20.
I believe this behavior is from back when MATLAB first introduced N-dimensional arrays (for N > 2) in MATLAB 5.0 (I think. That was well before the start of my time at MathWorks.)

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!