1 / 79

Logical Ops on Arrays

Logical Ops on Arrays. Saving time / avoiding loops Logical Operators Logical Operations Examples: numbers and letters. 1. General Idea. Applied to arrays , this solves problems such as… Find where the positives are positioned Count the number of negatives

lavina
Télécharger la présentation

Logical Ops on Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Logical Ops on Arrays Saving time / avoiding loops Logical Operators Logical Operations Examples: numbers and letters 1

  2. General Idea • Applied to arrays, this solves problems such as… • Find where the positives are positioned • Count the number of negatives • Delete the negatives, or maybe replace them by zeros • Replace letters by other letters In MATLAB, these can easily be solved in 1 or 2 lines, without the use of any loop and if statement (necessary in Java, C, C++..) • This is very useful in engineering, as tables of data always have to be filtered before use! • Note this is UNIQUE to MATLAB. No other software out there does this, so learn your loops and if statements anyway….

  3. General Idea • Suppose an array of values (vector or matrix) exists. The values came from sensors on a rocket, on a robot, on a car, etc... • How many sensors gave negative values? • 3 • Which sensors returned negative values? • #1 #5 #288 • What were the actual negative values? • 2003.2 100.2 552.45

  4. General Idea • Suppose a document with letters, words and sentences. (An array of letters). • How many capital letters are there? • Extract the capital letters, keep them in order. • At what positions are the capital letters located? • Encrypt the document by changing all a's to z's?

  5. General Idea • Recall the relational and logical operators (> >= < <= ~= == && ||)

  6. General Idea • Recall the relational and logical operators (> >= < <= ~= == && ||) • Logical Operations return a: • true – this can be evaluated as 1 • false – this can be evaluated as 0

  7. General Idea • Recall the relational and logical operators (> >= < <= ~= == && ||) • Logical Operations return a: • true – this can be evaluated as 1 • false – this can be evaluated as 0 2>5 is false, and can be evaluated as 0 2==4 is false, and can be evaluated as 0 6>=0 is true, and can be evaluated as 1

  8. General Idea • Recall the relational and logical operators (> >= < <= ~= == && ||) • Logical Operations return a: • true – this can be evaluated as 1 • false – this can be evaluated as 0 2>5 is false, and can be evaluated as 0 2==4 is false, and can be evaluated as 0 6>=0 is true, and can be evaluated as 1 X=4 %X is a scalar 0<=X && mod(X,2)==0 %positive and even is ______, and can be evaluated as __

  9. General Idea • Recall the relational and logical operators (> >= < <= ~= == && ||) • Logical Operations return a: • true – this can be evaluated as 1 • false – this can be evaluated as 0 X= [ … ]; %X is a not a scalar 0<=X & mod(X,2)==0 %positive and even In this section on arrays, only type 1 symbol: &, or | “element-wise” AND, OR

  10. Logical Operations • Suppose we want to know how many sensors gave negative values… What operation can be done to count the number of negatives? ________ When MATLAB applies an operation to each element in an array, we say that it is a “vector operation”. Here, MATLAB evaluates the condition on each element and assigns a 1 when true, a 0 when false. This creates a “logical vector”

  11. Logical Operations, cont. • How many sensors gave negative values? Simply use the built-in function.

  12. Logical Operations, cont. • How many sensors gave negative values? negatives is a logical vector. negatives is also called a 'mask'.

  13. Logical Operations • Which sensors returned negative values? How do we get the actual sensor numbers: #1 and #4?

  14. Logical Operations, cont. • MATLAB offers a built-in function that finds the index of values that make a condition true. In this application, “find where the sensor values are negatives”. whereNegatives is a vector of indices

  15. Logical Operations • Whatwere the actual negativevalues? • What are the actual values located at positions 1 and 4?

  16. Logical Operations • 3 methods possible: • Use the logical vectornegatives : sensor is the vector that has the values. It makes sense it comes first, since we are accessing those values.

  17. Logical Operations • 3 methods possible: • Use the logical vectornegatives : • Use the vector whereNegatives as indices.

  18. Logical Operations • 3 methods possible: • Use the condition directly (skip the “middleman” variables) • All of these are logical operations.

  19. Quick Summary • To get a logical vector: (0's and 1's) result1 = (x>=0); %var=condition; result = 0 1 0 1 0 0 0 1

  20. Quick Summary • To get a logical vector: (0's and 1's) result1 = (x>=0); %var=condition; result = 0 1 0 1 0 0 0 1 • To get the actual indices: result2 = find(x>=0); %var=find(condition); result = 2 4 8

  21. Quick Summary • To get a logical vector (mask): result1 = (x>=0); % var=condition; result = 0 1 0 1 0 0 0 1 • To get the indices: result2 = find(x>=0); %var=find(condition); result = 2 4 8 • To get the actual values: result = x(result1); %use the logical vector result = x(result2); %use the positions result = x(x>=0); %use the condition result = 34.2 55.5 9.9 Let's see examples…

  22. AIR FLOW Ex1 – Filtering Data www.trucktrend.com • Wind tunnels are commonly used to measure pressure changes, velocities changes, etc… Some use powerful electronic sensors, some use HD cameras and dyes in the air, others (like ERAU lab) use manometers. www.allamericanracers.com http://ceit.uq.edu.au ±h Static Fluid Equation: ΔP = ρgh

  23. Ex1: Manometer

  24. Ex1 – Filtering the readings % assume a vector of manometer reading % h difference in millimeters: -500<values<500 % indicate which tubes may not have not-worked, and % should be looked at (+/- 50 millimeter change) %if at least 1 was bad %display which tubes, and what were the readings % calculate difference in pressure at each point…

  25. Ex1 – Filtering the readings % assume a vector of manometer reading % h difference in millimeters: -500<values<500 ManoReadings = rand(1,20)*1000-500; % indicate which tubes may not have not-worked, and % should be looked at (+/- 50 millimeter change) shouldBeFixed = find(-50<=ManoReadings & ManoReadings<=50); if length(shouldBeFixed) ~=0 %if at least 1 was bad %display which tubes, and what were the readings end % calculate difference in pressure at each point…

  26. Ex1 – Filtering the readings % assume a vector of manometer reading % h difference in millimeters: -500<values<500 ManoReadings = rand(1,20)*1000-500; % indicate which tubes may not have not-worked, and % should be looked at (+/- 50 millimeter change) shouldBeFixed = find(-50<=ManoReadings & ManoReadings<=50); if length(shouldBeFixed) ~=0 %if at least 1 was bad disp('These tube numbers did not change much:') disp(shouldBeFixed) disp('Their values were:') disp(ManoReadings(shouldBeFixed)) end % calculate difference in pressure at each point…

  27. Ex1 - Output Logical operation: find() Logical operation: variableName(positions)

  28. Ex1 – Complete the program… % assume a vector of manometer reading % h difference in millimeters: -500<values<500 ManoReadings = rand(1,20)*1000-500; % indicate which tubes may not have not-worked, and % should be looked at (+/- 50 millimeter change) shouldBeFixed = find(-50<=ManoReadings & ManoReadings<=50); if length(shouldBeFixed) ~=0 %if at least 1 was bad disp('These tube numbers did not change much:') disp(shouldBeFixed) disp('Their values were:') disp(ManoReadings(shouldBeFixed)) end % calculate difference in pressure at each point… Replace by readings from machine!

  29. Ex2 – Sum all the positive evens % Suppose have a vector x of integers % Determine where "numbers is even" is true % Add up all the values that were even

  30. Ex2 – Sum all the positive evens % Suppose have a vector x of integers % Determine where "numbers is even" is true whereTrue = (mod(x,2)==0 & x>=0); % Add up all the values that were even result = sum(x(whereTrue)); Use element-wise AND

  31. Ex2 – Sum all the positive evens % Suppose have a vector x of integers % Determine where "numbers is even" is true whereTrue = (mod(x,2)==0 & x>=0); % Add up all the values that were even result = sum(x(whereTrue)); OR % Suppose have a vector x of integers % find the POSITIONS of the positive even numbers % in x, sum the numbers at these positions Use element-wise AND

  32. Ex2 – Sum all the positive evens % Suppose have a vector x of integers % Determine where "numbers is even" is true whereTrue = (mod(x,2)==0 & x>=0); % Add up all the values that were even result = sum(x(whereTrue)); OR % Suppose have a vector x of integers % find the POSITIONS of the positive even numbers positions = find(mod(x,2)==0 & x>=0); % in x, sum the numbers at these positions result = sum(x(positions)); Use only 1 symbol for AND and OR.

  33. Ex2 – Sum all the positive evens OR directly… % Suppose have a vector x of integers % Sum the positive, even numbers of x result = sum(x(mod(x,2)==0 & x>=0)); Use element-wise AND

  34. Ex3 – How about matrices? The client orders 100 pieces of part number 65. How much is his total bill, with a tax of 6.5%?  The part number and quantity can change, hard coding is not an option!

  35. Ex3 – Analyze table, cont. %ask user for part number, and quantity part_number = input('Enter part number: '); quantity = input('Enter quantity: '); %find the row of that part number %in table, get the actual unit price %calculate sub-total %add in taxes .

  36. Ex3 – Analyze table, cont. %ask user for part number, and quantity part_number = input('Enter part number: '); quantity = input('Enter quantity: '); %find the row of that part number [row, col] = find(table == part_number); %in table, get the actual unit price %calculate sub-total %add in taxes . >> [row, col] = find(table == part_number) row = 2 col = 1

  37. Ex3 – Analyze table, cont. %ask user for part number, and quantity part_number = input('Enter part number: '); quantity = input('Enter quantity: '); %find the row of that part number [row, col] = find(table == part_number); %in table, get the actual unit price unit_price = table(row,2); %calculate sub-total %add in taxes .

  38. Ex3 – Analyze table, cont. %ask user for part number, and quantity part_number = input('Enter part number: '); quantity = input('Enter quantity: '); %find the row of that part number [row, col] = find(table == part_number); %in table, get the actual unit price unit_price = table(row,2); %calculate sub-total sub_total = quantity * unit_price; %add in taxes total_bill = sub_total * (1+0.65); fprintf('Your bill adds up to: $%20.2f\n', total_bill)

  39. Ex3 – Analyze table, cont. • Note that this would not go well if the part_number's value is actually also one of the unit price!

  40. Ex3 – Analyze table, cont. • Note that this would not go well if the part_number's value is actually also one of the unit price! >> [row, col] = find(table == part_number) row = 2 3 col = 1 2

  41. Ex3 – Analyze table, cont. • Note that this would not go well if the part_number's value is actually also one of the unit price! %find the row of that part number, column1 ONLY row = find(table == part_number); >> [row, col] = find(table == part_number) row = 2 3 col = 1 2 (:,1) That's SLICING again!

  42. Ex4 – Works for strings too! • Simulate DNA with a string • GATACCAT… • The problem is: Generate the matching DNA string! • A-T • T-A • C-G • G-C Source: http://www.scq.ubc.ca

  43. Ex4 – Works for strings too! • Using a loop, and an if statement originalDNA ='GATACCAT'; matchDNA = []; %empty container for the matching DNA %loop for each letter for ctr = 1:length(originalDNA) if originalDNA(ctr) == 'T' %Where it is a 'T', make the 'A' matchDNA(ctr)='A'; elseif originalDNA(ctr) == 'A' matchDNA(ctr)='T'; elseif originalDNA(ctr) == 'C' matchDNA(ctr)='G'; elseif originalDNA(ctr) == 'G' matchDNA(ctr)='C'; end end

  44. Ex4 – Works for strings too! • Using a loop, and an if statement originalDNA ='GATACCAT'; matchDNA = []; %empty container for the matching DNA %loop for each letter for ctr = 1:length(originalDNA) if originalDNA(ctr) == 'T' %Where it is a 'T', make the 'A' matchDNA(ctr)='A'; elseif originalDNA(ctr) == 'A' matchDNA(ctr)='T'; elseif originalDNA(ctr) == 'C' matchDNA(ctr)='G'; elseif originalDNA(ctr) == 'G' matchDNA(ctr)='C'; end end length() is used to count the number of elements in a vector, here: number of characters

  45. Ex4 – Works for strings too! • Or.. In 5 lines! originalDNA ='GATACCAT' originalDNA = GATACCAT

  46. Ex4 – Works for strings too! • Or.. In 5 lines! originalDNA ='GATACCAT' %at the T's location, put an 'A' matchDNA(originalDNA =='T')='A' originalDNA = GATACCAT matchDNA = A A

  47. Ex4 – Works for strings too! • Or.. In 5 lines! originalDNA ='GATACCAT' %at the T's location, put an 'A' matchDNA(originalDNA =='T')='A' %at the A's location, put a T matchDNA(originalDNA =='A')='T' originalDNA = GATACCAT matchDNA = A A matchDNA = TAT TA

  48. Ex4 – Works for strings too! • Or.. In 5 lines! originalDNA ='GATACCAT' %at the T's location, put an 'A' matchDNA(originalDNA =='T')='A' %at the A's location, put a T matchDNA(originalDNA =='A')='T' %at the G's location, put a C matchDNA(originalDNA =='G')='C' %at the C's location, put a G matchDNA(originalDNA =='C')='G' originalDNA = GATACCAT matchDNA = A A matchDNA = TAT TA matchDNA = CTAT TA matchDNA = CTATGGTA

  49. One last keyword • end • It represents automatically the index of the last element in a vector

  50. Wrapping Up • The OR and AND operators are slightly updated • What does it mean: To “evaluate” • True can be evaluated as 1 • False can be evaluated as 0 • Logical vectors are vectors of 0's and 1's. • find(condition)returns a numerical vector of positions where the condition is true. • 3 methods to access (to refer to) the actual elements • It works with letters too. (encryption) • The keyword end (nothing to do with the if/switch/for/while) refers to the last element in the array. (note it will be black, not blue!)

More Related