using a while loop to add one to a vector
4 ビュー (過去 30 日間)
古いコメントを表示
I am supposed to use a while loop to add one to a vector. when i use this code it goes on for infinity but my bounds are set for when i is less than or equal to the length of the vector.
function [vec] = addOne(vec)
vec = [2 1 5 2]
l = length(vec)
i = 1:l
while i <= l
vec(i) = vec(i)+1
end
addOne = vec;
end
0 件のコメント
採用された回答
David Hill
2022 年 10 月 20 日
function vec = addOne(vec)
i=1;
while i<=length(vec)
vec(i) = vec(i)+1;
i=i+1;
end
end
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!