Switch signs to positive in the diagonal of a matrix

6 ビュー (過去 30 日間)
Jonas
Jonas 2022 年 11 月 21 日
コメント済み: Jonas 2022 年 11 月 21 日
Hi
So I have an already existing 6x6 matrix, but I would like to make sure that the diagonals are all positive.
Thus, how do I transform/replace the diagonal of my matrix to be all positive?
I have tried to seach for, how to replace the diagonal with a vector, but without luck.
If I could replace the original diagonal with the absolute diagonal, this would also do it.

採用された回答

David Hill
David Hill 2022 年 11 月 21 日
m=randi(10,6)-5
m = 6×6
0 2 1 2 5 -3 1 -3 1 -1 5 -2 4 1 -4 2 4 -1 -3 2 1 -4 2 -2 3 5 3 1 5 4 3 5 5 -2 5 0
d=abs(diag(m));
M=m.*~eye(size(m))+diag(d)
M = 6×6
0 2 1 2 5 -3 1 3 1 -1 5 -2 4 1 4 2 4 -1 -3 2 1 4 2 -2 3 5 3 1 5 4 3 5 5 -2 5 0
  1 件のコメント
Jonas
Jonas 2022 年 11 月 21 日
Thank you so much, David! Worked just the way I wanted to! :-)

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

その他の回答 (0 件)

カテゴリ

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