Modify off diagonal elements of Matrix without looping
Mostrar comentarios más antiguos
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!
Respuesta aceptada
Más respuestas (1)
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
3 comentarios
Chet Sharma
el 17 de Ag. de 2022
Bruno Luong
el 17 de Ag. de 2022
Chet Sharma
el 17 de Ag. de 2022
Categorías
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!