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

20 vues (au cours des 30 derniers jours)
MathWorks Support Team
MathWorks Support Team le 27 Fév 2012
Commenté : Walter Roberson le 5 Juil 2022
Why do I get the following error message :
Matrix must be square.

Réponse acceptée

MathWorks Support Team
MathWorks Support Team le 27 Fév 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 commentaire
Walter Roberson
Walter Roberson le 5 Juil 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

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Aucun tag saisi pour le moment.

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by