Replace diagonals in a matrix
조회 수: 8 (최근 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!