using a while loop to add one to a vector
조회 수: 8 (최근 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!