How do I change the values of diag(matrix) ?

24 ビュー (過去 30 日間)
abbyeit
abbyeit 2021 年 10 月 25 日
コメント済み: abbyeit 2021 年 10 月 25 日
Hello!
I am wondering why I cant change the values of diag(matrix) to different values.
>> matrix =
3 0 0
0 4 0
0 0 5
>> diag(matrix) = 5;
What I am trying to do with that code is change the values 3, 4, 5 in the matrix to all have the value 5. But it does not work.
How can I make this work?
Thanks in advance!

採用された回答

Image Analyst
Image Analyst 2021 年 10 月 25 日
Try this (using eye() to get a mask of the diagonal elements):
matrix = [...
3 0 0
0 4 0
0 0 5]
diagonalMask = logical(eye(size(matrix)))
matrix(diagonalMask) = 5
  1 件のコメント
abbyeit
abbyeit 2021 年 10 月 25 日
Thanks!

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by