Why do I get the error message 'Matrix must be square.' ?
27 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
MathWorks Support Team
el 27 de Feb. de 2012
Comentada: Walter Roberson
el 5 de Jul. de 2022
Why do I get the following error message :
Matrix must be square.
Respuesta aceptada
MathWorks Support Team
el 27 de Feb. de 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 comentario
Walter Roberson
el 5 de Jul. de 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
Más respuestas (0)
Ver también
Categorías
Más información sobre Linear Algebra en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!