How best and most efficient way to build this matrix

조회 수: 1 (최근 30일)
Scott Banks
Scott Banks 2023년 9월 3일
댓글: Matt J 2023년 9월 4일
Hi guys,
I am wondering what is the best and most efficient way to build this matrix using loops and functions? Rather than inputting the numbers manually?
A = [-2 1 0 0 0 0 0 0 0 0
6 -4 1 0 0 0 0 0 0 0
-4 6 -4 1 0 0 0 0 0 0
1 -4 6 -4 1 0 0 0 0 0
0 1 -4 6 -4 1 0 0 0 0
0 0 1 -4 6 -4 1 0 0 0
0 0 0 1 -4 6 -4 0 0 0
0 0 0 0 1 -4 6 -4 1 0
0 0 0 0 0 1 -4 6 -4 1
0 0 0 0 0 0 0 0 1 -2]
A = 10×10
-2 1 0 0 0 0 0 0 0 0 6 -4 1 0 0 0 0 0 0 0 -4 6 -4 1 0 0 0 0 0 0 1 -4 6 -4 1 0 0 0 0 0 0 1 -4 6 -4 1 0 0 0 0 0 0 1 -4 6 -4 1 0 0 0 0 0 0 1 -4 6 -4 0 0 0 0 0 0 0 1 -4 6 -4 1 0 0 0 0 0 0 1 -4 6 -4 1 0 0 0 0 0 0 0 0 1 -2
Many thanks
  댓글 수: 3
Matt J
Matt J 2023년 9월 3일
Has A(7,8) deliberately been made 0 instead of 1? If so, it is not clear what the pattern is supposed to be.
Scott Banks
Scott Banks 2023년 9월 3일
No, Matt, it should be 1. Sorry for the typo

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

채택된 답변

Matt J
Matt J 2023년 9월 3일
[C,R]=deal(zeros(1,10));
C(1:4)=[-4 6 -4 1];
R(1:2)=[-4,1];
A=toeplitz(C,R);
A(1)=-2;
A(end,:)=flip(A(1,:))
A = 10×10
-2 1 0 0 0 0 0 0 0 0 6 -4 1 0 0 0 0 0 0 0 -4 6 -4 1 0 0 0 0 0 0 1 -4 6 -4 1 0 0 0 0 0 0 1 -4 6 -4 1 0 0 0 0 0 0 1 -4 6 -4 1 0 0 0 0 0 0 1 -4 6 -4 1 0 0 0 0 0 0 1 -4 6 -4 1 0 0 0 0 0 0 1 -4 6 -4 1 0 0 0 0 0 0 0 0 1 -2
  댓글 수: 3
Matt J
Matt J 2023년 9월 4일
@Scott Banks for a full explanation, I suggest the documentation links provided by Walter.

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

추가 답변 (1개)

Torsten
Torsten 2023년 9월 3일
이동: Torsten 2023년 9월 3일
Experiment with "spdiags":
If first and last row appear different from your matrix, you can change them subsequently.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by