How can I store the values in a matrix while using for loop?
1 次查看(过去 30 天)
显示 更早的评论
Hi, I am a bit confused, how to store the values in a matrix while using for loop. Here is the my code
k=1.5:0.1:3;
ws=zeros(1,length(k));
dof=zeros(1,length(k));
for i=1.5:0.1:3
ws=22+4+7+i;
dof(i)=2+ ws;
end
I want to store the values pf 'ws' and 'dof' in matrix. Thanks
0 个评论
回答(2 个)
Andrei Bobrov
2017-7-12
编辑:Andrei Bobrov
2017-7-12
k=1.5:0.1:3;
n = numel(k);
ws=zeros(1,n);
dof=zeros(1,n);
for ii=1:n
ws(ii)=22+4+7+k(ii);
dof(ii)=2+ ws(ii);
end
or just
ws = 33 + k;
dof = ws + 2;
0 个评论
alice
2017-7-12
You have to give the position where you want to write, like this:
k = 1.5:0.1:3;
ws = zeros(1,length(k));
dof = zeros(1,length(k));
for cpt = 1:length(k)
ws(cpt) = 22+4+7+k(cpt);
dof(cpt)= 2+ws(cpt);
end
But you don't need a loop to do this and it would be better to do simply:
k = 1.5:0.1:3;
ws = 22+4+7+k;
dof = 2+ws;
0 个评论
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!