How do I call the elements in an array to be counted?
조회 수: 1 (최근 30일)
이전 댓글 표시
I am trying to create a while loop which loops over the elements in the array sumCheck and stops looping when it reaches an element that is greater than 9. I also want to count how many times the loop loops, so I've used count.
count = 0
while sumCheck <= 9 && count<=length(sumCheck)
count = count + 1
end
count
So far I've written this, but I feel like I'm missing something. I'm not sure how to call for all the elements in sumCheck to be less than 9.
댓글 수: 0
답변 (2개)
Image Analyst
2020년 11월 26일
Try this
count = 1;
while sumCheck(count) <= 9
count = count + 1
end
fprintf('Exited loop when count = %d and sumCheck(%d) = %f.\n', count, count, sumCheck(count))
댓글 수: 2
Image Analyst
2020년 11월 26일
Yes, because you said "stops looping when it reaches an element that is greater than 9." so it will stop at 10. If you want the one before that, simply subtract 1 from count after the loop.
count = 1;
while sumCheck(count) <= 9
count = count + 1
end
fprintf('Exited loop when count = %d and sumCheck(%d) = %f.\n', count, count, sumCheck(count))
count = count - 1;
or change <= to <.
참고 항목
카테고리
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!