How to make an if : (NOT this AND that OR here AND there OR etc AND etc) statement
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Image Analyst
2015-1-27
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
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!