matlab logical indexing greg reese, ph.d research computing support group academic technology...

51
MATLAB Logical Indexing Greg Reese, Ph.D Research Computing Support Group Academic Technology Services Miami University

Upload: peter-mcgee

Post on 03-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

MATLAB

Logical Indexing

Greg Reese, Ph.D

Research Computing Support Group

Academic Technology Services

Miami University

MATLAB Logical Indexing

© 2010-2013 Greg Reese. All rights reserved 2

3

Logical indexing

One method of selecting elements in a matrix or vector is to choose those that meet some criterion, e.g.,

– the elements that are greater than zero– the elements that are not more than three

standard deviations from the mean

MATLAB does this by logical indexing and relational operators

4

Logical indexingMATLAB has the usual relational operators

To get more information, type help ops

Symbol Operation== Equal~= Not equal< Less than> Greater than

<= Less than or equal

>= Greater than or equal

5

Logical indexing

The result of comparing two scalars with a relational operator is a logical value. A logical value can only be true or false

MATLAB represents true by the number 1 and false by 0• Can also use the keywords true and false

6

Logical indexing

Example>> x=5;>> x>5ans = 0>> x<=5ans = 1>> x==5ans = 1>> x~=5ans = 0

7

Logical indexing

When compare vector to vector, or vector to scalar, MATLAB • Does elementwise comparison

– Compares scalar to every element of vector– Compares corresponding elements of vectors

• Vectors must be same dimension

• Result is vector of logical values• Result has same dimension as vector in

comparison

8

Logical indexing

Example>> x=8:12x = 8 9 10 11 12>> x>10ans = 0 0 0 1 1>> x==11ans = 0 0 0 1 0>> x>=7ans = 1 1 1 1 1

9

Logical indexing

TipIt helps to picture in your mind that the result of a logical comparison

1. Is a vector

2. Has a 0 or 1 corresponding to each original element

>> x=8:12x = 8 9 10 11 12>> x>10ans = 0 0 0 1 1

10

Logical indexingTry It>> times = [ 9.7 9.9 10.1 12.8 ];Mark times less than nine

>> times < 9ans = 0 0 0 0Mark times greater than 12

>> times > 12ans = 0 0 0 1Mark times equal to 9.9

>> times == 9.9ans = 0 1 0 0

==, not =

11

Logical indexing

MATLAB provides logical operations such as AND, OR, NOT, etc.• Can compare two scalars• If compare scalar to vector or vector to

vector, do elementwise comparison– If two vectors, must both be same dimension

To get more information, type help ops

12

Logical indexingSymbol Operation

& AND – true if both elements are nonzero, false otherwise

| OR – true if one or both elements are nonzero, false otherwise

xor() EXCLUSIVE OR – true if exactly one element is nonzero, false otherwise

~ NOT – makes nonzero element zero and zero element one

any() True if any element of a vector is nonzero, false otherwise

all() True if all elements of a vector are nonzero, false otherwise

13

Logical indexing

ExampleChild – 12 or less years

Teenager – more than 12 and less than 20 years

Adult – 20 or more years

>> age=[45 47 15 13 11]age = 45 47 15 13 11

14

Logical indexingExampleage = 45 47 15 13 11Who is a teenager?

>> age>=13 ans = 1 1 1 1 0>> age<=19ans = 0 0 1 1 1>> age>=13 & age<=19ans = 0 0 1 1 0

15

Logical indexingExample>> age=[45 47 15 13 11]age = 45 47 15 13 11

Who is not a teenager?>> ~(age>=13 & age<=19)ans = 1 1 0 0 1

Who is an adult or a child?>> age>19 | age<13ans = 1 1 0 0 1

16

Logical indexingExample>> age=[45 47 15 13 11]age = 45 47 15 13 11

Are there any teenagers?>> any( age >= 13 & age <= 19 )ans = 1

Are all the people teenagers?>> all( age >= 13 & age <= 19 )ans = 0

17

Logical indexingFor even more power you can use logical values as subscripts in a vector or matrix. This is called logical indexing or logical subscripting.

To perform logical subscripting on a vector x, pass it (in parentheses) a logical vector of the same dimension. The result is a vector of all the elements of x for which the logical vector is true.

18

Logical indexing

Example>> numbers = 1:8numbers = 1 2 3 4 5 6 7 8% find even numbers greater than 4>> matches = ...rem( numbers, 2 ) == 0 & numbers > 4matches = 0 0 0 0 0 1 0 1>> numbers(matches)ans = 6 8

19

Logical indexing

Examplenumbers = 1 2 3 4 5 6 7 8matches = 0 0 0 0 0 1 0 1Think of numbers(matches) as pulling out of numbers all elements that have a 1 in the corresponding element of matchesnumbers = 1 2 3 4 5 6 7 8

matches = 0 0 0 0 0 1 0 1>> numbers(matches) ans = 6 8

20

Logical indexingTry It>> age=[45 47 15 13 11]age = 45 47 15 13 11

Mark teenagers with 1, non-teens with 0>> age>=13 & age<=19ans = 0 0 1 1 0

How old are the teenagers?>> age( age >= 13 & age <= 19)ans = 15 13

21

Logical indexing

TipIf you’re going to use the results of a calculation a lot, compute it once and save it in a variable.

22

Logical indexing

Example>> age=[45 47 15 13 11];>> weight=[202 151 113 125 94];>> teenager = age>=13 & age<=19;>> age(teenager)ans = 15 13>> weight(teenager)ans = 113 125

23

Logical indexing

TipSince relational and logical operations return 1 if they meet a criterion and 0 if they don’t, you can count the number of elements in a vector that meet a criterion by finding the sum of the logical vector result.• The function sum returns the sum

of a vector’s elements

24

Logical indexingExample>> age=[45 47 15 13 11];>> weight=[202 151 113 125 94];

How many teenagers are there?>> age >= 13 & age <= 19ans = 0 0 1 1 0>> sum( age >= 13 & age <= 19 )ans = 2

25

Logical indexingExample>> age=[45 47 15 13 11];>> weight=[202 151 113 125 94];How many people weigh more than 200 lbs?

>> weight > 200ans = 1 0 0 0 0>> sum( weight > 200 )ans = 1

26

Logical indexing

Try It>> age=[45 47 15 13 11];>> weight=[202 151 113 125 94];

How many adults are there?

How many children are there?

>> sum( age >= 20 )ans = 2

>> sum( age < 13 )ans = 1

27

Logical indexing

Try It>> age=[45 47 15 13 11];>> weight=[202 151 113 125 94];

How many adults weigh more than 200 lbs?

How many children weigh less than 100 lbs?

>> sum( age >= 20 & weight > 200 )ans = 1

>> sum( age < 13 & weight < 100 )ans = 1

28

Logical indexing

TipOne good use of logical indexing is the detection and removal of outliers

– Outliers are whacky data values, i.e., numbers way off the mean– Conventional definition is any number more than 3 standard deviations from mean, i.e., x is an outlier if

3x

29

Logical indexing

Tip>> data = randn( 1, 100 );>> [ theMax maxIx ] = max( data )theMax = 3.5784maxIx = 9>> [ theMin minIx ] = min( data )theMin = -2.9443minIx = 35% put in new min/max but keep old ones>> data(maxIx+1) = 988.64;>> data(minIx+1) = -2000;

30

Logical indexing

Tip - show outliers>> data( abs(data-mean(data)) > 3*std(data) )ans = 1.0e+003 * 0.9886 -2.0000

31

Logical indexing

Remember, can delete elements by specifying indexes to delete and setting to [], e.g.,

>> v=2:2:10v = 2 4 6 8 10>> v([2 5]) = []v = 2 6 8

32

Logical indexing

Can delete elements that meet certain criteria in an analogous way:

v(logical_vector) = []ExampleDelete all elements that are a multiple of 4

>> v=2:2:10v = 2 4 6 8 10>> v(rem(v,4)==0) = []v = 2 6 10

33

Logical indexingTip - can remove outliers% show outliers>> data( abs(data-mean(data))>3*std(data) )ans = 1.0e+003 * 0.9886 -2.0000

% remove outliers

>> data( abs(data-mean(data))>3*std(data) )=[];>> length( data )ans = 98>> max( data )ans = 3.5784>> min( data )ans = -2.9443

34

Logical indexing

The find function is related to logical indexing. It returns the indexes of the elements of a vector that are nonzero. You can use those indexes to select the elements of a vector that meet a criterion

find is most helpful when used on logical vectors

35

Logical indexing

Example>> age=[45 47 15 13 11];>> find( age >= 13 & age <= 19 )ans = 3 4

How old are the teenagers?>> age( find( age>=13 & age <=19 ))ans = 15 13

36

Logical indexing

Example – use find>> age=[45 47 15 13 11];>> weight=[202 151 113 125 94];

How old is the second teenager?

>> indexes = find(age >= 13 & age <= 19)indexes = 3 4>> age( indexes(2) )ans = 13

37

Logical indexing

Try It – use find>> age=[45 47 15 13 11];>> weight=[202 151 113 125 94];

How much does the second teenager weigh?

>> indexes = find( age >= 13 & age <= 19 )indexes = 3 4>> weight( indexes(2) )ans = 125

38

Logical IndexingNaN (Not a Number) • Built-in constant (can also write nan)• Represents result of mathematically

undefined operations, such as 0 / 0 or ∞ - ∞ (infinity minus infinity)

• Any logical or relational comparison of two NaN’s returns false, except ~=

• isnan(x) returns true if x is NaN and false otherwise

39

Logical IndexingNaN used to mark missing data points

Example

Two students didn’t show up for a quiz:>> grades=[3 10 8 NaN 0 8 7 5 NaN 6];>> isnan( grades )ans = 0 0 0 1 0 0 0 0 1 0>> find( isnan( grades ) )ans = 4 9

40

Logical IndexingNaN messes up numerical computations. Remove NaN’s from data first

Example>> grades=[3 10 8 NaN 0 8 7 5 NaN 6];>> mean(grades)ans = NaN>> validGrades = grades( ~isnan(grades) )validGrades = 3 10 8 0 8 7 5 6>> mean( validGrades )ans = 5.8750

41

Logical indexingTip If you're going to work a lot with data that has missing values, the statistics toolbox can help you. It has functions that compute common statistics and automatically ignore NaN's. They are: nancov, nanmax, nanmean, nanmedian, nanmin, nanstd, nansum, nanvar

Example>> v = [ 10 NaN 6 2 NaN ];>> mean( v )ans = NaN>> nanmean( v )ans = 6

42

Logical IndexingIn the fall of 2004 non-freshmen students at Miami University were surveyed. One of the questions was to write the number of hours of anti-alcohol education the students had received at Miami. The responses are numbers from 0 to 4 with 0 to 3 being the actual number of hours and 4 representing more than 3 hours. Some students did not respond at all.

Load the answers to the question with the command

>> allAnswers = load( 'survey.txt' )

43

Logical Indexing

Try ItIn the survey, students who didn’t answer the question are represented by NaN. Answer the following questions (without nanxxx functions):• How many students were in the

survey?• How many students did not answer

the survey question?

44

Logical indexing

>> allAnswers=load('survey.txt');

% Number of students in survey?>> length(allAnswers)ans = 531

% Number of students not responding?>> sum(isnan(allAnswers))ans = 59

45

Logical IndexingTry ItMake a vector for only the students who answered. Use it to answer the following questions about the amount of college alcohol-prevention education the students had.

Are all the values in your new vector legal, i.e., between 0 and 4 inclusive?

46

Logical indexing

>> good=allAnswers( ~isnan(allAnswers) );

% Are all responses legal (>=0 and <=4)?>> all(good>=0 & good<=4 )ans = 1

47

Logical IndexingTry It• What percentage didn’t have any

prevention education? • For every student who had the

maximum amount of prevention education, how many had none?

• What was the average number of hours of prevention education for students who had 1, 2, or 3 such hours?

48

Logical indexing% Percentage uneducated?>> 100*sum(good==0)/length(good)ans = 39.6186

% uneducated / maximally educated?>> sum(good==0)/sum(good==4)ans = 1.8515

% average for those with some but not all>> mean( good( good>=1 & good<=3 ) )ans = 2.0435

49

Logical indexing

FINAL POINT

Note that we have answered all the questions

1. Without using if-statements

2. Without using loops

Logical indexing makes cleaner, faster code!

50

Logical indexing

Questions?

51

The End