Replace diagonals in a matrix
8 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hasan Hassoun
il 19 Gen 2021
Commentato: Image Analyst
il 23 Gen 2021
Hello every one,
How to replace the upper and lower part of a n*n matrix with zeros (The upper part starts from "diagonal+2" until n while the lower part starts from "diagonal-2" until n)?
1 Commento
Image Analyst
il 23 Gen 2021
Original question is below in case he deletes it like he's done before:
Replace diagonals in a matrix
Hello every one,
How to replace the upper and lower part of a n*n matrix with zeros (The upper part starts from "diagonal+2" until n while the lower part starts from "diagonal-2" until n)?
Risposta accettata
Più risposte (1)
David Goodmanson
il 19 Gen 2021
Modificato: David Goodmanson
il 19 Gen 2021
Hi Hasan,
here is one way
r = rand(7,7)
n = size(r,1);
m = (n-1)/2;
a = (-m:m)-(-m:m)';
r(abs(a)>1)=0
assuming the main diagonal is number 0, and the zeros start on the +-2nd diagonal, otherwise adjust the '1' on the last line of code accordingly
Vedere anche
Categorie
Scopri di più su Operating on Diagonal Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!