Math 445: Comparisons and logical operations on vectors
% Logical operations ||, &&, and xor operator on single pairs
% of logical (boolean) values. Examples:
% demo of ||, logical OR
if 3 == 4 || 5 == 5; disp('true'); else disp('false'); end
true
% demo of &&, logical AND
if 3 == 4 && 5 == 5; disp('true'); else disp('false'); end
false
% demo xor, logical EXCLUSIVE OR
if xor(3 == 4, 5 == 5); disp('true'); else disp('false'); end
true
if xor(4 == 4, 5 == 5); disp('true'); else disp('false'); end
false
% Can also do comparisons and logical operations on vectors.
% First set a couple vectors x and y for demos
x = [1 2 3]
x =
1 2 3
y = [5 2 9]
y =
5 2 9
% Vector comparisons compare all the elements of the vectors
% Which elements of x are equal to the corresponding elements of y?
x == y
ans =
0 1 0
% Which elements of x are not equal to the corresponding elements of y?
x ~= y
ans =
1 0 1
% Which elements of x are greater or equal to the corresponding elements of y?
x >= y
ans =
0 1 0
% Now do some vectorized logical operations
% What elements are true (1) in both vectors?
[0 1 0] & [1 0 1]
ans =
0 0 0 % none of them!
% What elements are true in either vector?
[0 1 0] | [1 0 1]
ans =
1 1 1 % of them!
% Recall our x and y vectors
x =
1 2 3
y
y =
5 2 9
% In which elements is x greater than y?
x > y
ans =
0 0 0
% In which elements is x less than y?
x < y
ans =
1 0 1
% Repeat on some random vectors
x = rand(1,6)
x =
0.9224 0.7204 0.4806 0.7310 0.6328 0.7180
y = rand(1,6)
y =
0.8384 0.9191 0.3769 0.9563 0.0431 0.2091
x < y
ans =
0 1 0 1 0 0
% Set x to be a random vector with a Gaussian distribution around x=0
% (in order to get both positive and negative values).
x = randn(1,5)
x =
-0.7887 -2.1134 2.0821 1.8451 -0.7676
% How many positive elems are there in x?
% Well, we know how to test for positivity elementwise...
x > 0
ans =
0 0 1 1 0
% To count the number of positive elements, just apply sum to the prev result
sum(x > 0)
ans =
2
% Set a component of x to zero, for another deom
x(2) = 0
x =
-0.7887 0 2.0821 1.8451 -0.7676
% How many elems of x are not zero?
sum(x ~= 0)
ans =
4
% Which elems of x are greater than zero
x > 0
ans =
0 0 1 1 0
% What are the positive elements in x? Do this in pieces
x
x =
-0.7887 0 2.0821 1.8451 -0.7676
% This will tell us which elems are positive
x > 0
ans =
0 0 1 1 0
% Note the type of x>0: It's a vector of logical values
class(x > 0)
ans =
logical
% We can access elements of x by the vector of logical values
% First set n to be the vector of 0s and 1s marking where the
% positive elements are
n = x > 0
n =
0 0 1 1 0
% Now extract the positive elems of x using the logical n vector
x(n)
ans =
2.0821 1.8451
% Equivalently, we can do this without setting logical vector in a variable
x(x>0)
ans =
2.0821 1.8451
% Similarly, can get the negative values of x this way...
x(x<0)
ans =
-0.7887 -0.7676
% ...the zero values of x this way...
x(x==0)
ans =
0
% ...and the nonzero values of x this way.
x(x~=0)
ans =
-0.7887 2.0821 1.8451 -0.7676