필터 지우기
필터 지우기

Creating a Tridiagonal Matrix with varying values

조회 수: 14 (최근 30일)
Jonathan Miles
Jonathan Miles 2020년 10월 2일
답변: David Hill 2020년 10월 2일
Hello, I am trying to create a tridiagonal matrix with varying values. As shown in the image below
the main diagonal is able to have varying value, but the code only works if the offsetting diagonals are constant values. I am attempting to have all 3 diagonals be varying numerals any help would be greatly appreciated. I will attach my code below.
function [A,b] = ok(k,m)
A=[k];
n=size(A,2)
b=[m];
if size(b,2) < 1
b=[m];
else
b=[m]';
j=size(b,2);
end
N = n;
a = -k;
d =4;
c =4;
M = zeros(N,N)
M( 1:1+N:N*N) = a
M(N+1:1+N:(N*N)-1) = d
M( 2:1+N:N*N-N) = c
As shown above a can =-k , but when I set d or c = to -k I will recieve an error.
Thank you.

채택된 답변

David Hill
David Hill 2020년 10월 2일
x=diag(-1:-1:-8)+diag(1:7,-1)+diag(randi(10,1,7),1);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by