Matlab - logical operators Flashcards
what are these signs?
>
<
==
~=
Greater than
Less than
Is equal to
Is not equal to
what are these signs in Matlab?
& | ~
they are logical AND, OR and NOT
when you use just logical operators what do you get in return?
an index array consisting of 1s and 0s
what would the output be?
» A = [1 5 3 4 8 3];
» B = A>2
B = 0 1 1 1 1 1 (0 is false, 1 is true)
what would the output be for this?»_space;
» A = [1 5 3 4 8 3];
C = A<5
C = 1 0 1 1 0 1
if
» A = [1 5 3 4 8 3];
» B = A>2
B = 0 1 1 1 1 1
C = A<5
C = 1 0 1 1 0 1
what is D = B & C
D = 0 0 1 1 0 1
how would you logically index D into A? what would be the output?
> > A = [1 5 3 4 8 3];
B = A>2
B = 0 1 1 1 1 1
C = A<5
C = 1 0 1 1 0 1
D = B & C
D = 0 0 1 1 0 1
E = A(D)
E = 3 4 3
You can convert a logical index into a subscript index
using find, which tells you the non-zeros values of the logical.
F = find(D)
F = 3 4 6
you can use logical indexing when one variable is used to categorise another
example:
data = [4 14 6 11 3 14 8 17 17 12 10 18];
cat = [1 3 2 1 2 2 3 1 3 2 3 1];
how would you find where cat is 2? and their corresponding values?
> > cat2 = cat==2
cat2 = 0 0 1 0 1 1 0 0 0 1 0 0
data2 = data(cat2);
data2 = 6 3 14 12
how would you find the mean for the category 2 data?
mdat2 = mean(data2)