Switch signs to positive in the diagonal of a matrix

3 views (last 30 days)
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.

Accepted Answer

David Hill
David Hill on 21 Nov 2022
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

More Answers (0)

Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!