Modify off diagonal elements of Matrix without looping
조회 수: 24 (최근 30일)
이전 댓글 표시
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
채택된 답변
James Tursa
2022년 8월 17일
You could use logical indexing to get at the off-diagonal elements. E.g.,
A(~eye(size(A))) = skalar;
추가 답변 (1개)
Bruno Luong
2022년 8월 17일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!