我的代码如下:
a=zeros(5,1);
f=zeros(5,5);
for i=1:5
a(i,1)=5;
f(:,i)=a;
end
我希望每个循环的结果都是矩阵f的每一列。我的意思是f = [a(1)a(2)a(3)a(4)a(5)]其中a(i)来自for循环中的每个循环。但结果是:
5 5 5 5 5
0 5 5 5 5
0 0 5 5 5
0 0 0 5 5
0 0 0 0 5
我是matlab的新手。如果你告诉我哪里错了,我们将不胜感激。
答案 0 :(得分:0)
这样做
a=zeros(5,1);
f=zeros(5,5);
a(:,1)=5;
f(1,:)=transpose(a);
答案 1 :(得分:0)
I see that in line 4 you have a(:,1)=5. Since it is on a loop the value of a continues to expand so that you first have a(1)=5, a(2)= 5 5, a(3)= 5 5 5, etc. What you want to do is specify what specific value of a you want in your matrix f. You can do this by:
a=zeros(5,1);
f=zeros(5);
for i=1:5
a(i,1)=5;
f(i,i)=a(i,1); end
disp(f)