I've been writing a code, a small part of which checks whether some of the inputs sum to 1 (as they're all proportions of a whole). If they do, the code continues. If not, the code stops and throws up an error message.
a = 0.4;b = 0.3;c = 0.2;d = 0.1;total = [a,b,c,d];if sum(total) ~= 1 warning('Sum of a,b,c,d must be 1.') returnend
The example I've given here DOES NOT WORK, even though a+b+c+d = 1. It throws up the error message and stops the script.
To be clear, this works fine for a,b,c and d = 0.25. It works for a = 0.5, b = 0.4, c and d = 0.05. It doesn't work for the example I've given, and it doesn't work for a,b,c = 0.3, d = 0.1. I haven't checked other combinations exhaustively.
Any help would be greatly appreciated, thanks.
Best Answer