MATLAB Answers

Effective matrix defining (diagonals)

조회 수: 3(최근 30일)
Nitzan Haviv
Nitzan Haviv 2021년 8월 4일
답변: Nitzan Haviv 2021년 8월 4일
hi, i would like to get some help with defining the next matrix in the easiest way. i know the diag() function but i didnt find a way the define the non-main diagonals.

채택된 답변

Stephen
Stephen 2021년 8월 4일
N = 7;
M = toeplitz([-2,1,zeros(1,N-2)])
M = 7×7
-2 1 0 0 0 0 0 1 -2 1 0 0 0 0 0 1 -2 1 0 0 0 0 0 1 -2 1 0 0 0 0 0 1 -2 1 0 0 0 0 0 1 -2 1 0 0 0 0 0 1 -2
  댓글 수: 2
Stephen
Stephen 2021년 8월 4일
@Nitzan Haviv: my pleasure! Remember to click the accept button if my answer helped you!

댓글을 달려면 로그인하십시오.

추가 답변(1개)

Nitzan Haviv
Nitzan Haviv 2021년 8월 4일
btw i found another way
when N is the dimention of the square matrix
d_sec = ones(1,N-1);
d_main= ones(1,N).*-2;
H = diag(d_main)+diag(d_sec,-1) + diag(d_sec,1);

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by