Storing all Loop results in an array?

조회 수: 10 (최근 30일)
Grace
Grace 2023년 3월 12일
댓글: Walter Roberson 2023년 3월 12일
counter = 0;
max = 10;
UserInput = 1;
while(counter < max && UserInput == 1)
A = input('\n Enter A value here ->');
if (A < 0)
fprintf ('\n \t ERROR: Enter A value that is more than zero, start again');
elseif (A >= 0)
fprintf ('\n \t Value Accepted! \n');
B = input('\n Enter B value here ->');
if (B < 0)
fprintf ('\n \t ERROR: Enter B value that is more than zero, start again');
elseif (B >= 0)
fprintf ('\n \t Value Accepted! \n');
C = A + B;
%%I am trying to store the C in an array for everytime the loop is ran
fprintf('\n \t Result is %d \n', C)
UserInput = input("\n Would you like to exit the program? [1 for no, 2 for yes] ->");
end %For A loop
end %For B loop
end %For the while loop
fprintf('\n Goodbye \n')

채택된 답변

Matt J
Matt J 2023년 3월 12일
result(counter)=C;
  댓글 수: 2
Grace
Grace 2023년 3월 12일
Resulted in this error :Array indices must be positive integers or logical values.
Walter Roberson
Walter Roberson 2023년 3월 12일
counter = counter + 1;
result(counter) = C;

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by