how many times to divide by zero? in matlab

In MATLAB, dividing by zero will result in an error message displaying "Divide by zero." To determine how many times you are dividing by zero in your code, you can use try-catch block to catch the exception that occurs when the code tries to divide by zero. Here is an example code snippet:

main.m
numerator = [1 2 3 4 5];
denominator = [6 7 0 10 11];

for ii = 1:length(numerator)
    try
        result(ii) = numerator(ii)/denominator(ii);
    catch ME
        if strcmp(ME.identifier,'MATLAB:divideByZero')
            disp('Divide by zero occurred!')
        else
            rethrow(ME)
        end
    end
end
318 chars
15 lines

In this example, we have created two vectors, numerator and denominator, and we are iterating through them using the for-loop. When the code tries to divide by zero, it will throw an exception that is caught by the try-catch block. If the exception is a "Divide by zero" exception, we will display a message indicating that a divide by zero has occurred. If the exception is not a "Divide by zero" exception, we rethrow the exception so that we can see the full error message.

By counting the number of times the "Divide by zero occurred!" message is displayed, we can determine how many times the code tries to divide by zero.

gistlibby LogSnag