can anybody give me a example showing how to avoid division by 0 by using try-catch?
조회 수: 5 (최근 30일)
이전 댓글 표시
can anybody give me a example showing how to avoid division by 0 by using try-catch? thank you very much!
댓글 수: 2
Jan
2011년 10월 7일
TRY-CATCH does not *avoid* the division by zero. In opposite: The term "try" means, that MATLAB tries to calculate the division and calls the CATCH block, if an error occurs.
채택된 답변
Grzegorz Knor
2011년 10월 7일
Dividing by zero does not return an error in Matlab. So I suggest you to use if - else instead try - catch:
for k = -5:5
if k~=0
x = 1./k;
disp(x)
else
disp('you try divide by zero!')
end
end
댓글 수: 3
Walter Roberson
2011년 10월 7일
I do not remember the details at the moment, but I seem to recall that division by zero is noticed and reported via a warning or error (I do not remember which), except that that warning or error is turned off by default.
추가 답변 (1개)
Walter Roberson
2011년 10월 7일
Don't do that. Do like I recommended earlier: calculate the denominator and assign the result to a variable, and test the range of the variable, and take appropriate corrective action if it is smaller than you want.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!