Replace diagonals in a matrix
10 次查看(过去 30 天)
显示 更早的评论
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 个评论
Image Analyst
2021-1-23
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)?
采纳的回答
更多回答(1 个)
David Goodmanson
2021-1-19
编辑:David Goodmanson
2021-1-19
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!