How can I store the values in a matrix while using for loop?

조회 수: 1 (최근 30일)
Tayyab Mehmood
Tayyab Mehmood 2017년 7월 12일
편집: Andrei Bobrov 2017년 7월 12일
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

답변 (2개)

Andrei Bobrov
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;

alice
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;

카테고리

Help CenterFile Exchange에서 Direction of Arrival Estimation에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by