logical indexing

logical indexing - Logical Indexing Greg Reese, Ph.D...

Info iconThis preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon
Logical Indexing Greg Reese, Ph.D Research Computing Support Group Academic Technology Services Miami University
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Logical Indexing © 2010 Greg Reese. All rights reserved 2
Background image of page 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
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Logical indexing MATLAB 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
Background image of page 4
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
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Logical indexing Example >> x=5; >> x>5 ans = 0 >> x<=5 ans = 1 >> x==5 ans = 1 >> x~=5 ans = 0
Background image of page 6
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
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 Logical indexing Example >> x=8:12 x = 8 9 10 11 12 >> x>10 ans = 0 0 0 1 1 >> x==11 ans = 0 0 0 1 0 >> x>=7 ans = 1 1 1 1 1
Background image of page 8
9 Logical indexing Tip It 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:12 x = 8 9 10 11 12 >> x>10 ans = 0 0 0 1 1
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10 Logical indexing Try It >> times = [ 9.7 9.9 10.1 12.8 ]; Mark times less than nine >> times < 9 ans = 0 0 0 0 Mark times greater than 12 >> times > 12 ans = 0 0 0 1 Mark times equal to 9.9 >> times == 9.9 ans = 0 1 0 0 ==, not =
Background image of page 10
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
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
12 Logical indexing Symbol 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
Background image of page 12
Logical indexing Example Child – 12 or less years
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 11/04/2011 for the course MME 414 at Miami University.

Page1 / 50

logical indexing - Logical Indexing Greg Reese, Ph.D...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online