How can I store the values in a matrix while using for loop?
15 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (2)
Andrei Bobrov
am 12 Jul. 2017
Bearbeitet: Andrei Bobrov
am 12 Jul. 2017
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 Kommentare
alice
am 12 Jul. 2017
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 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!