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...

 채택된 답변

Rick Rosson
Rick Rosson 2011년 7월 28일
Please try the following:
M = 8;
N = 5;
X = randn(M,N);
idx = eye(M,N);
Y = (1-idx).*X;
Z = X(~idx);
HTH.
Best, Rick
Responding to your comment:
To multiply the non-diagonal elements by 2, please try:
A = [2 3 5;3 6 8;5 8 4];
idx = eye(size(A));
idx = idx + 2*(1-idx);
Y = idx.*A;
HTH.
Best, Rick

댓글 수: 4

eg: i have 3x3 matrix. A = [2 3 5;3 6 8;5 8 4]. then , i want to multiply all the off-diagonal entries by 2. Can i use ur recommended code in my situation? tq for ur cooperation.
Do you want to keep the diagonal entries as well, or do you want to zero them out? Also, do you want the result to be a 3x3 matrix, or a 6x1 column vector (eliminating the 3 diagonal elements)? Please let me know.
i wanna keep the diagonal entries as well..and i also want the result also in 3x3 matrix..tq
I modified my answer above.

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Nathan Greco
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

eg: i have 3x3 matrix. A = [2 3 5;3 6 8;5 8 4]. then , i want to multiply all the off-diagonal entries by 2. is it clear enough? tq for ur cooperation.
i used the 1st one from ur answer.. tq very2 much... :)
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;
OR: A.*2-diag(diag(A)) would work.
yup.. A.*2-diag(diag(A)) is the one i'm searching for.. i forgot to mentioned that i wanna keep the diagonal entries as well.. anyway, tq..

댓글을 달려면 로그인하십시오.

dor eivensitz
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

Please open up a new question for this, and provide a short example in that new question.

댓글을 달려면 로그인하십시오.

James Tursa
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
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
M = 4x4
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
idx = ~eye(size(M)) % logical index indicating non-diagonal entries
idx = 4x4 logical array
0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0
result = sum(M(idx),'all') % sum the non-diagonal entries
result = 102

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

태그

질문:

2011년 7월 28일

댓글:

2024년 7월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by