create a function to evaluate when apply a test, test should be applied when the difference between the previous result and the new result is higher than 10% in matlab

You can define a function with the following inputs: previous result, new result, and a percentage threshold. The function will compute the difference between the two results and check if it is higher than the percentage threshold of the previous result.

Here is an example implementation:

main.m
function shouldApplyTest = evaluateTest(prevResult, newResult, threshold)
    diff = abs((newResult - prevResult) / prevResult);
    shouldApplyTest = (diff > threshold);
end
175 chars
5 lines

This function first computes the difference as the absolute value of the ratio between the two results. It then compares this difference to the threshold value and returns a boolean indicating whether a test should be applied.

You can use this function with your own values like this:

main.m
prevResult = 100;
newResult = 120;
threshold = 0.1;  % 10%

shouldApplyTest = evaluateTest(prevResult, newResult, threshold);
126 chars
6 lines

This will return true since the difference between 100 and 120 is 20%, which is higher than the threshold of 10%.

Note that this implementation assumes that the two results are nonzero. You may want to add additional checks if zero is a possible value.

related categories

gistlibby LogSnag