Why do I get the error message 'Matrix must be square.' ?

245 views (last 30 days)
Why do I get the following error message :
Matrix must be square.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Feb 2012
Explanation:
You are attempting to perform an operation that is only defined for square matrices or matrices with equal number of rows and columns.
Common causes:
You are attempting to use an element-wise operator on a matrix that is nonsquare, but you are using the linear algebra operator instead of the element-wise operator. For example, you use the ^ operator (taking the power of a matrix) rather than .^ (which takes the power of each element of the matrix).
Solution:
Examine the line listed in the error message and verify the matrix whose power you want to take is square, or that you are using the appropriate operator.
Example demonstrating this error:
MatrixMustBeSquare.m
  1 Comment
Walter Roberson
Walter Roberson on 5 Jul 2022
[3 4]^2
is an example. It uses the matrix power operator, and would be equivalent to
[3 4] * [3 4]
but remember that the * operation is matrix multiplication (inner product), not element-by-element multiplication
A fair portion of the time when you get the message, you should probably be using the .^ operation instead of ^ . But sometimes it is just a mistake in the logic so that the array is not the square size that the programmer was expecting

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Tags

No tags entered yet.

Products

Community Treasure Hunt

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

Start Hunting!