How do I access and modify only the non diagonal entries in a matrix?
이전 댓글 표시
hello.. i hv a question. what is the command to call the non diagonal entries of a matrix? tq very much...
채택된 답변
추가 답변 (4개)
Nathan Greco
2011년 7월 28일
If tmp is your matrix, try:
tmp-diag(diag(tmp)) %works only with square matrices
OR
triu(tmp,1)+tril(tmp,-1)
Both of these set the diagonal entries to zero, essentially ignoring them. If this isn't what you want, please clarify.
댓글 수: 5
srycandy
2011년 7월 28일
srycandy
2011년 7월 28일
Nathan Greco
2011년 7월 28일
You could either do the above methods and multiply (.*) the result by 2, or use indexing as such:
idx = find(~eye(size(A))); %get NON diagonal entries
A(idx) = A(idx).*2;
Nathan Greco
2011년 7월 28일
OR: A.*2-diag(diag(A)) would work.
srycandy
2011년 7월 28일
dor eivensitz
2017년 11월 2일
0 개 추천
i want to keep both diagonal in matrix size i don't know, defined by n, and all other elements to multiply by them self plus 1. tnx
댓글 수: 1
James Tursa
2017년 11월 2일
Please open up a new question for this, and provide a short example in that new question.
James Tursa
2017년 11월 2일
Another way for a square matrix:
M = your matrix
x = ~logical(eye(size(M)));
M(x) = 2*M(x); % <-- or whatever function you want on the rhs using M(x)
Niba Kainat
2024년 7월 18일
0 개 추천
hi can someone tell me how to compute sum of no diagonal entries.
댓글 수: 1
M = magic(4) % some square matrix
idx = ~eye(size(M)) % logical index indicating non-diagonal entries
result = sum(M(idx),'all') % sum the non-diagonal entries
카테고리
도움말 센터 및 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!