logical indexing

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

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

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

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

View Full Document
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

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

View Full Document
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
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

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

View Full Document
6 Logical indexing Example >> x=5; >> x>5 ans = 0 >> x<=5 ans = 1 >> x==5 ans = 1 >> x~=5 ans = 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

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

View Full Document
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
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

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

View Full Document
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 =
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

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

View Full Document
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
Logical indexing Example Child – 12 or less years

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

View Full Document
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
Ask a homework question - tutors are online