How do I specify the diagonal elements of a matrix?

32 visualizaciones (últimos 30 días)
farida khurshid
farida khurshid el 13 de Feb. de 2011
Comentada: Sebastien de Kort el 11 de Sept. de 2020
I have some values, taken from another matrix. How do I specify them to be the diagonals of a new matrix?

Respuestas (2)

Matt Tearle
Matt Tearle el 13 de Feb. de 2011
To do a subscripted assignment into the diagonal of a matrix, you can use linear indexing:
A(1:n+1:end) = v
(where v is an n-element vector and n is the number of rows of A). So, for example,
A(1:n+1:end) = diag(B)
copies the diagonal of B into A.
  5 comentarios
Walter Roberson
Walter Roberson el 5 de Mzo. de 2020
n:n-1:end-n+1 for backwards diagonal on a square matrix
Sebastien de Kort
Sebastien de Kort el 11 de Sept. de 2020
A(logical(fliplr(eye(n)))) = diag(B)

Iniciar sesión para comentar.


Bruno Luong
Bruno Luong el 13 de Feb. de 2011
Take a look at DIAG function
help diag

Categorías

Más información sobre Operating on Diagonal Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by