How to add values to diagonal of a matrix
14 次查看(过去 30 天)
显示 更早的评论
k=1;
n=6;
for i=1:6
for j=1:6
A(i,j)=?
I need to have values in the diagonal going from 1:6 but I keep getting an error.
0 个评论
回答(1 个)
David Hill
2019-8-26
x=diag(1:6);
Is the diagonial matrix with 1:6 on the diagonal.
2 个评论
David Hill
2019-8-26
I am not sure what you want to do, but why not just change your input?
k=zeros(6);
k(1,1)=1;
x=diag(1:6);
k+x;
Yes, the sizes need to be the same, but why not just add zeros to match the size?
另请参阅
类别
在 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!