Getting a diag function

조회 수: 6 (최근 30일)
Mark Loui
Mark Loui 2021년 3월 18일
편집: Matt J 2021년 3월 18일
Hi there im stuck with this problem and really would like help here
n=length(x) %Let says is 20
x=rand(n,1);
y=rand(n,1);
RHS_empty=ones(ny,1);
iter_h=0;
for i=1:n-1
h(i)=x(i+1)-x(i);
iter_h=iter_h+1;
Ma_Sp=diag(1 h(i)) 2(h(i+1)*h(i))
end
I like to create a diagonal matrix with the interval with the specific things,
The diagonal matrix size is depending on the h(i), meaning h19, i like to get the matrix to be 20x20 with 1 at the first lane and h1 2h2*h1 h2 2h3*h2 hn 2h(i+1)*h(i) for the second coloum and it goes on for the third coloum and the fourth all the way to the 20th column, i am stuck on this matter and would appreciate your help
  댓글 수: 3
Mark Loui
Mark Loui 2021년 3월 18일
ermm are you familar with Natural Splines Boundary Condition, my main goal is to create a matrix as such. If not
It means i like to create a matrix that is diagonal
in matlab code wise is
A=[1 0 0 0 ;0 h1 2(h2+h1) h2 2(h3+h2) ;0 0 h2 2(h3+h2); 0 0 0 h4] This is what i meant by the entire sentence
So if n=19, there should be a 20x20 matrix fill with the listed diagonal matrix above, this is what i mean. Hope this clears things up
Jan
Jan 2021년 3월 18일
편집: Jan 2021년 3월 18일
The shown matrix A is not diagonal. Diagonal matrices contaoin non-zero elements on the diagonal only. You can create them with the command diag() .
There is a large, if not infitine number of ways, to fill a 20x20 matrix with a 4x4 matrix.
Maybe you are looking for the kron() command:
x = [1,2;3,4]
x =
1 2
3 4
kron(eye(3), x)
ans =
1 2 0 0 0 0
3 4 0 0 0 0
0 0 1 2 0 0
0 0 3 4 0 0
0 0 0 0 1 2
0 0 0 0 3 4

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by