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.

回答(1 个)

David Hill
David Hill 2019-8-26
x=diag(1:6);
Is the diagonial matrix with 1:6 on the diagonal.
  2 个评论
Brock Eubanks
Brock Eubanks 2019-8-26
that gives me an error, where the size of the left one is 1-by-1 and the size of the right one is 6-by-6. I just thought you could add a 1 to k each time for C11, C22, etc. to get 1, 2, 3, 4, 5, 6 for the main diagonal.
David Hill
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 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