a simple Question about loops

2 ビュー (過去 30 日間)
Rica
Rica 2014 年 3 月 12 日
編集済み: Rica 2014 年 3 月 12 日
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

採用された回答

Chris C
Chris C 2014 年 3 月 12 日
編集済み: Chris C 2014 年 3 月 12 日
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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by