How do I access and modify only the non diagonal entries in a matrix?
92 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
srycandy
am 28 Jul. 2011
Kommentiert: Voss
am 18 Jul. 2024
hello.. i hv a question. what is the command to call the non diagonal entries of a matrix? tq very much...
0 Kommentare
Akzeptierte Antwort
Rick Rosson
am 28 Jul. 2011
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 Kommentare
Weitere Antworten (4)
Nathan Greco
am 28 Jul. 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 Kommentare
dor eivensitz
am 2 Nov. 2017
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 Kommentar
James Tursa
am 2 Nov. 2017
Please open up a new question for this, and provide a short example in that new question.
James Tursa
am 2 Nov. 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)
0 Kommentare
Niba Kainat
am 18 Jul. 2024
hi can someone tell me how to compute sum of no diagonal entries.
1 Kommentar
Voss
am 18 Jul. 2024
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
Siehe auch
Kategorien
Mehr zu Array Geometries and Analysis finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!