Switch signs to positive in the diagonal of a matrix

6 次查看(过去 30 天)
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by