using a while loop to add one to a vector
7 次查看(过去 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!