Modify off diagonal elements of Matrix without looping
35 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Chet Sharma
am 17 Aug. 2022
Kommentiert: Chet Sharma
am 17 Aug. 2022
Hello
I have a MXM matrix. Is there a way to set off diagonal elements to a specified value (.e.g. I want all off-diagonal elements to be some constant "k'), without looping through the matrix? I know there is a function "diag"....was hoping to find something that does the opposite....not sucessful so far.
Thanks!
0 Kommentare
Akzeptierte Antwort
James Tursa
am 17 Aug. 2022
You could use logical indexing to get at the off-diagonal elements. E.g.,
A(~eye(size(A))) = skalar;
Weitere Antworten (1)
Bruno Luong
am 17 Aug. 2022
M = 4;
A = rand(M);
A(1:size(A,1)+1:end) = 10, % This is how you change the diagonal of A using linear indexing
Siehe auch
Kategorien
Mehr zu Operating on Diagonal Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!