How do you make the diagonal and certain off-diagonal elements of a matrix zero?

73 visualizzazioni (ultimi 30 giorni)
I have a square matrix and would like to make the diagonal elements zero, as well as select elements that are off-diagonal. For the latter, I only want the elements next to (one removed from) the diagonal to be zero, e.g., I would want A(5,6) and A(6,5) to be 0 for a matrix A. I can do the former part for the diagonal by
A_new = A - diag(diag(A));
But how do I do the part for the off-diagonal?

Risposta accettata

Walter Roberson
Walter Roberson il 14 Feb 2023
S = spdiags(Bin,d,A) replaces the diagonals in A specified by d with the columns of Bin.
... after which you would full()
This could be used directly to replace diagonals -1:1 with zeros. Or you could use spdiags twice to extract multiple diagonals simultaneously and create a matrix with just those, and subtract from the original... the generalization of your current code.
  2 Commenti
L'O.G.
L'O.G. il 14 Feb 2023
Thanks, but how would I replace diagonals -1:1 with zeros for the full matrix?
Walter Roberson
Walter Roberson il 14 Feb 2023
n = 9;
A = rand(n)
A = 9×9
0.8787 0.9000 0.3293 0.7781 0.9511 0.4674 0.5774 0.0860 0.1346 0.5791 0.2043 0.2635 0.7183 0.4309 0.0203 0.9671 0.0253 0.7339 0.8793 0.8386 0.7464 0.8748 0.7670 0.0196 0.9867 0.3659 0.9148 0.2191 0.4816 0.5973 0.9866 0.5886 0.8485 0.0188 0.9976 0.1732 0.9975 0.8073 0.9317 0.4963 0.1424 0.6094 0.0971 0.9048 0.4144 0.9345 0.9627 0.8312 0.5215 0.4967 0.4954 0.6387 0.0993 0.1622 0.0785 0.8886 0.3785 0.6098 0.2500 0.0021 0.2668 0.6566 0.8886 0.9968 0.6688 0.3537 0.3499 0.1719 0.3704 0.5856 0.3745 0.9827 0.7165 0.0935 0.0269 0.7502 0.6695 0.5330 0.0463 0.2427 0.8659
newA = full(spdiags(zeros(9,3), -1:1, A))
newA = 9×9
0 0 0.3293 0.7781 0.9511 0.4674 0.5774 0.0860 0.1346 0 0 0 0.7183 0.4309 0.0203 0.9671 0.0253 0.7339 0.8793 0 0 0 0.7670 0.0196 0.9867 0.3659 0.9148 0.2191 0.4816 0 0 0 0.8485 0.0188 0.9976 0.1732 0.9975 0.8073 0.9317 0 0 0 0.0971 0.9048 0.4144 0.9345 0.9627 0.8312 0.5215 0 0 0 0.0993 0.1622 0.0785 0.8886 0.3785 0.6098 0.2500 0 0 0 0.8886 0.9968 0.6688 0.3537 0.3499 0.1719 0.3704 0 0 0 0.7165 0.0935 0.0269 0.7502 0.6695 0.5330 0.0463 0 0

Accedi per commentare.

Più risposte (1)

Torsten
Torsten il 14 Feb 2023
Modificato: Torsten il 14 Feb 2023
n = 9;
A = rand(n)
A = 9×9
0.1775 0.9064 0.8468 0.0887 0.5042 0.2522 0.9969 0.4269 0.8669 0.5412 0.5086 0.7654 0.8466 0.7660 0.1267 0.5925 0.4179 0.2214 0.3812 0.9799 0.0524 0.7062 0.0993 0.2886 0.3217 0.2389 0.3402 0.4666 0.4828 0.9887 0.4107 0.0607 0.3509 0.1436 0.0533 0.8215 0.9084 0.9898 0.4007 0.2539 0.0025 0.2415 0.1024 0.8357 0.0494 0.0396 0.8522 0.2150 0.2113 0.0925 0.9267 0.3426 0.5880 0.2066 0.3261 0.8049 0.8634 0.7682 0.4854 0.6717 0.9052 0.2654 0.1252 0.4238 0.8070 0.3574 0.1515 0.5762 0.4482 0.5737 0.8663 0.7392 0.2939 0.9826 0.1098 0.7926 0.0099 0.5213 0.6287 0.4949 0.1724
for i=1:n-1
A(i,i+1) = 0.0;
A(i+1,i) = 0.0;
end
A
A = 9×9
0.1775 0 0.8468 0.0887 0.5042 0.2522 0.9969 0.4269 0.8669 0 0.5086 0 0.8466 0.7660 0.1267 0.5925 0.4179 0.2214 0.3812 0 0.0524 0 0.0993 0.2886 0.3217 0.2389 0.3402 0.4666 0.4828 0 0.4107 0 0.3509 0.1436 0.0533 0.8215 0.9084 0.9898 0.4007 0 0.0025 0 0.1024 0.8357 0.0494 0.0396 0.8522 0.2150 0.2113 0 0.9267 0 0.5880 0.2066 0.3261 0.8049 0.8634 0.7682 0.4854 0 0.9052 0 0.1252 0.4238 0.8070 0.3574 0.1515 0.5762 0.4482 0 0.8663 0 0.2939 0.9826 0.1098 0.7926 0.0099 0.5213 0.6287 0 0.1724
  1 Commento
Walter Roberson
Walter Roberson il 14 Feb 2023
The above can be vectorized. The vectorized version is faster than the loop or than my spdiags() solution.
The spdiags() solution is more compact, and so in some sense easier to understand. On the other hand, spdiags() is not one of the more common routines, so fewer people would be expected to understand it immediately without looking at the reference work; and while they might understand it for a while after that, if you were to have the same people look at the code (say) 6 months later, they would probably have lost the understanding. The loop solution is the more robust to that kind of longer term mental decay.
n = 9;
A = rand(n)
A = 9×9
0.6719 0.8061 0.2292 0.2202 0.1543 0.1305 0.1906 0.5847 0.7707 0.3742 0.3372 0.9419 0.7160 0.4509 0.9439 0.6036 0.0207 0.4701 0.1070 0.4909 0.9614 0.4621 0.8391 0.7562 0.8774 0.1968 0.9709 0.9364 0.1200 0.7590 0.4416 0.5981 0.0527 0.5583 0.9255 0.3805 0.2359 0.5982 0.7043 0.3346 0.7068 0.0744 0.5488 0.5272 0.3085 0.2901 0.2437 0.2179 0.0531 0.6202 0.6724 0.4453 0.7965 0.8494 0.3323 0.2516 0.7856 0.6836 0.4200 0.0216 0.7360 0.2761 0.9327 0.6585 0.5026 0.5395 0.2308 0.8373 0.2470 0.6693 0.7792 0.1291 0.1974 0.5096 0.5655 0.2327 0.1521 0.2926 0.0624 0.8403 0.5790
Acopy = A;
tic
for i=1:n-1
A(i,i) = 0.0;
A(i,i+1) = 0.0;
A(i+1,i) = 0.0;
end
toc
Elapsed time is 0.006974 seconds.
A
A = 9×9
0 0 0.2292 0.2202 0.1543 0.1305 0.1906 0.5847 0.7707 0 0 0 0.7160 0.4509 0.9439 0.6036 0.0207 0.4701 0.1070 0 0 0 0.8391 0.7562 0.8774 0.1968 0.9709 0.9364 0.1200 0 0 0 0.0527 0.5583 0.9255 0.3805 0.2359 0.5982 0.7043 0 0 0 0.5488 0.5272 0.3085 0.2901 0.2437 0.2179 0.0531 0 0 0 0.7965 0.8494 0.3323 0.2516 0.7856 0.6836 0.4200 0 0 0 0.9327 0.6585 0.5026 0.5395 0.2308 0.8373 0.2470 0 0 0 0.1974 0.5096 0.5655 0.2327 0.1521 0.2926 0.0624 0 0.5790
A = Acopy;
tic
n1 = n+1;
A(1:n1:end) = 0;
A(2:n1:end) = 0;
A(n1:n1:end) = 0;
toc
Elapsed time is 0.003191 seconds.
A
A = 9×9
0 0 0.2292 0.2202 0.1543 0.1305 0.1906 0.5847 0.7707 0 0 0 0.7160 0.4509 0.9439 0.6036 0.0207 0.4701 0.1070 0 0 0 0.8391 0.7562 0.8774 0.1968 0.9709 0.9364 0.1200 0 0 0 0.0527 0.5583 0.9255 0.3805 0.2359 0.5982 0.7043 0 0 0 0.5488 0.5272 0.3085 0.2901 0.2437 0.2179 0.0531 0 0 0 0.7965 0.8494 0.3323 0.2516 0.7856 0.6836 0.4200 0 0 0 0.9327 0.6585 0.5026 0.5395 0.2308 0.8373 0.2470 0 0 0 0.1974 0.5096 0.5655 0.2327 0.1521 0.2926 0.0624 0 0
A = Acopy;
tic
A = full(spdiags(zeros(9,3), -1:1, A));
toc
Elapsed time is 0.013573 seconds.
A
A = 9×9
0 0 0.2292 0.2202 0.1543 0.1305 0.1906 0.5847 0.7707 0 0 0 0.7160 0.4509 0.9439 0.6036 0.0207 0.4701 0.1070 0 0 0 0.8391 0.7562 0.8774 0.1968 0.9709 0.9364 0.1200 0 0 0 0.0527 0.5583 0.9255 0.3805 0.2359 0.5982 0.7043 0 0 0 0.5488 0.5272 0.3085 0.2901 0.2437 0.2179 0.0531 0 0 0 0.7965 0.8494 0.3323 0.2516 0.7856 0.6836 0.4200 0 0 0 0.9327 0.6585 0.5026 0.5395 0.2308 0.8373 0.2470 0 0 0 0.1974 0.5096 0.5655 0.2327 0.1521 0.2926 0.0624 0 0

Accedi per commentare.

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by