a simple Question about loops

1 vue (au cours des 30 derniers jours)
Rica
Rica le 12 Mar 2014
Modifié(e) : Rica le 12 Mar 2014
Hi All
for exemple:
%%
%
a=[1 2 3 5 4 8];
v=[]
v(1)=a(1);
for k=2:length(a)
if(k<3)
v(k)=v(k)*(k-1);
else
v(k)=(v(k-1)+(v(k))/3);
end
end
i get this error: Attempted to access v(2); index out of bounds because numel(v)=1.
why? thanks for yor help

Réponse acceptée

Chris C
Chris C le 12 Mar 2014
Modifié(e) : Chris C le 12 Mar 2014
You initialize v incorrectly. Try it this way....
a=[1 2 3 5 4 8];
v=ones(length(a));
v(1)=a(1);
for k=2:length(a)
if(k<3)
v(k)=v(k)*(k-1);
else
v(k)=(v(k-1)+(v(k))/3);
end
end

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by