MATLAB provide two types of logical operators:
Element-wise − these operators operate/work on corresponding elements of logical arrays.
Short-circuit − these operators operate/work on scalar, logical expressions.
Element-wise logical operators operate by taking single element at a time of logical arrays. The symbols |, & and ~ are the logical array operators OR, AND, and NOT respectively.
Short-circuit logical operators do short-circuiting on logical operations. The symbols || and&& are the logical short-circuit operators OR and AND respectively.
These can be better understood by taking the following examples.
Example1.
a = 5;
b = 20;
if ( a && b )
disp('Line 1 - Condition is true');
end
if ( a || b )
disp('Line 2 - Condition is true');
end>
Example2.
% lets change in the value of a and b
a = 0;
b = 10;
if ( a && b )
disp('Line 3 - Condition is true');
else
disp('Line 3 - Condition is not true');
end
if (~(a && b))
disp('Line 4 - Condition is true');
end
MATLAB always gives the (& ) operator preference over the | operator. Although MATLAB generally evaluates expressions from left to right, the expression a|b&c is solved as a|(b&c). It may be a good idea to use brackets to explicitly show the intended precedence of statements containing combinations of & and |.
These logical operators have the M-file function equivalents, as shown.
Logical Operation | Equivalent Function |
---|---|
A & B | and(A,B) |
A | B | or(A,B) |
~A | not(A) |
Matlabsolutions.com provides guaranteed satisfaction with a
commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain
experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support
to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been
empanelled after extensive research and quality check.
Matlabsolutions.com provides undivided attention to each Matlab
assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services
include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work
done at the best price in industry.