MATLAB: How to make an if : (NOT this AND that OR here AND there OR etc AND etc) statement

if statement

Hi there,
I want to make an if statement with the following format: There are only four allowed states, (1,0) (-1,0) (0,-1) (0,1). The code below is what I have setup and I need someway to convert the second into a NOT (this) statement, it is currently a (this) statement. I mean that aside from those four states, nothing else is allowed. For interest there are 9 combinations possible with 4 allowed for 2 inputs (3^n rule). Thank you for the help.
if UD ~= -1 || UD ~= 0 || UD ~= 1 || LR ~= -1 || LR ~= 0 || LR ~= 1
error('UD/LR must either -1, 0 or 1');
elseif UD == 1 && LR == 0 || UD == -1 && LR == 0 || UD == 0 && LR == -1 || UD == 0 && LR == 1
error('UD/LR must be some combination of -1, 0 and 1');
end

Best Answer

Just do this:
[~, allowed] = ismember([UD, LR], allowedStates, 'rows')
Here's a full demo:
% Define allowed states
allowedStates = [1,0; -1,0; 0,-1; 0,1]
% Show just one example.
UD = -1; % For example;
LR = 40; % For example
% See if [UD, LR] is one of the allowed states

[~, allowed] = ismember([UD, LR], allowedStates, 'rows')
% Now, as a further example test all possible states
% and show whether they are allowed or not.
for UD = -1:1
for LR = -1:1
% See if [UD, LR] is one of the allowed states
[~, allowed] = ismember([UD, LR], allowedStates, 'rows')
if allowed
fprintf('UD=%d, LR=%d is allowed\n', UD, LR);
else
fprintf('UD=%d, LR=%d is not allowed\n', UD, LR);
end
end
end
Related Question