필터 지우기
필터 지우기

how do I create the below Matrix?

조회 수: 1 (최근 30일)
Brian Nuttall
Brian Nuttall 2015년 3월 24일
답변: Konstantinos Sofos 2015년 3월 24일
please see the attached image i need to create a tridiagonal nxn matrix using the diag function. i know how to make it this way but thats not what is required.
>> A=[2 -1 0 0 0;-1 2 -1 0 0;0 -1 2 -1 0;0 0 -1 2 -1; 0 0 0 -1 2]
A =
2 -1 0 0 0
-1 2 -1 0 0
0 -1 2 -1 0
0 0 -1 2 -1
0 0 0 -1 2
  댓글 수: 1
James Tursa
James Tursa 2015년 3월 24일
"... not what is required."
Is this homework? Have you reviewed the doc on diag to see how to get values into the off-diagonals?

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

채택된 답변

Konstantinos Sofos
Konstantinos Sofos 2015년 3월 24일
Hi,
As James wrote you:Have you reviewed the doc on diag to see how to get values into the off-diagonals?
Mathworks offers an excellent documentation for such topics with plenty of examples.
You could use this
function Out = mydiag(n)
%n size of matrix Out
Out= diag(-1*ones(1,n-1),-1) + diag(2*ones(1,n)) + diag(-1*ones(1,n-1),1);
end
>> mydiag(5)
ans =
2 -1 0 0 0
-1 2 -1 0 0
0 -1 2 -1 0
0 0 -1 2 -1
0 0 0 -1 2

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Linear Algebra에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by