Getting a diag function
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
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 Center 和 File Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!