How do I specify the diagonal elements of a matrix?

39 ビュー (過去 30 日間)
farida khurshid
farida khurshid 2011 年 2 月 13 日
コメント済み: Sebastien de Kort 2020 年 9 月 11 日
I have some values, taken from another matrix. How do I specify them to be the diagonals of a new matrix?
  1 件のコメント
Hassan Rezaee
Hassan Rezaee 2016 年 8 月 19 日
You may use the function spdiags

サインインしてコメントする。

回答 (2 件)

Matt Tearle
Matt Tearle 2011 年 2 月 13 日
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 件のコメント
Walter Roberson
Walter Roberson 2020 年 3 月 5 日
n:n-1:end-n+1 for backwards diagonal on a square matrix
Sebastien de Kort
Sebastien de Kort 2020 年 9 月 11 日
A(logical(fliplr(eye(n)))) = diag(B)

サインインしてコメントする。


Bruno Luong
Bruno Luong 2011 年 2 月 13 日
Take a look at DIAG function
help diag

カテゴリ

Help Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by