How do I specify the diagonal elements of a matrix?

37 vues (au cours des 30 derniers jours)
farida khurshid
farida khurshid le 13 Fév 2011
I have some values, taken from another matrix. How do I specify them to be the diagonals of a new matrix?
  1 commentaire
Hassan Rezaee
Hassan Rezaee le 19 Août 2016
You may use the function spdiags

Connectez-vous pour commenter.

Réponses (2)

Matt Tearle
Matt Tearle le 13 Fév 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 commentaires
Walter Roberson
Walter Roberson le 5 Mar 2020
n:n-1:end-n+1 for backwards diagonal on a square matrix
Sebastien de Kort
Sebastien de Kort le 11 Sep 2020
A(logical(fliplr(eye(n)))) = diag(B)

Connectez-vous pour commenter.


Bruno Luong
Bruno Luong le 13 Fév 2011
Take a look at DIAG function
help diag

Catégories

En savoir plus sur Operating on Diagonal Matrices dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by