Replace diagonals in a matrix
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hasan Hassoun
el 19 de En. de 2021
Comentada: Image Analyst
el 23 de En. de 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 comentario
Image Analyst
el 23 de En. de 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)?
Respuesta aceptada
Matt J
el 19 de En. de 2021
For example,
A=rand(10),
mask=tril( triu( true(size(A)), -2 ), +2);
B=A.*mask
Más respuestas (1)
David Goodmanson
el 19 de En. de 2021
Editada: David Goodmanson
el 19 de En. de 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
Ver también
Categorías
Más información sobre Operating on Diagonal Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!