How do I access and modify only the non diagonal entries in a matrix?
Mostrar comentarios más antiguos
hello.. i hv a question. what is the command to call the non diagonal entries of a matrix? tq very much...
Respuesta aceptada
Más respuestas (4)
Nathan Greco
el 28 de Jul. de 2011
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 comentarios
srycandy
el 28 de Jul. de 2011
srycandy
el 28 de Jul. de 2011
Nathan Greco
el 28 de Jul. de 2011
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
el 28 de Jul. de 2011
OR: A.*2-diag(diag(A)) would work.
srycandy
el 28 de Jul. de 2011
dor eivensitz
el 2 de Nov. de 2017
0 votos
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 comentario
James Tursa
el 2 de Nov. de 2017
Please open up a new question for this, and provide a short example in that new question.
James Tursa
el 2 de Nov. de 2017
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
el 18 de Jul. de 2024
0 votos
hi can someone tell me how to compute sum of no diagonal entries.
1 comentario
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
Categorías
Más información sobre Operating on Diagonal Matrices 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!