Download - Logical Ops on Arrays
![Page 1: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/1.jpg)
Logical Ops on Arrays
Saving time / avoiding loops1.Logical Operators
2.Logical Operations
3.Examples: numbers and letters
11
![Page 2: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/2.jpg)
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….
2
![Page 3: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/3.jpg)
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...
1. How many sensors gave negative values? 3
2. Which sensors returned negative values? #1 #5 #288
3. What were the actual negative values? 2003.2 100.2 552.45
3
![Page 4: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/4.jpg)
General Idea
Suppose a document with letters, words and sentences. (An array of letters).
1. How many capital letters are there?
2. Extract the capital letters, keep them in order.
3. At what positions are the capital letters located?
4. Encrypt the document by changing all a's to z's?
4
![Page 5: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/5.jpg)
General Idea Recall the relational and logical operators
(> >= < <= ~= == && ||)
5
![Page 6: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/6.jpg)
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
6
![Page 7: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/7.jpg)
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
7
![Page 8: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/8.jpg)
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 __8
![Page 9: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/9.jpg)
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
9
In this section on arrays, only type 1 symbol: &, or |
“element-wise” AND, OR
![Page 10: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/10.jpg)
Logical Operations
1. Suppose we want to know how many sensors gave negative values…
What operation can be done to count the number of negatives? ________ 10
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”
![Page 11: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/11.jpg)
Logical Operations, cont.
11
Simply use the built-in function.
1. How many sensors gave negative values?
![Page 12: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/12.jpg)
Logical Operations, cont.
12
negatives is a logical vector.
negatives is also called a 'mask'.
1. How many sensors gave negative values?
![Page 13: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/13.jpg)
Logical Operations
13
How do we get the actual sensor numbers: #1 and #4?
2. Which sensors returned negative values?
![Page 14: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/14.jpg)
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”.
14
whereNegatives is a vector of indices
![Page 15: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/15.jpg)
Logical Operations
15
3. What were the actual negative values? What are the actual values located at positions 1 and 4?
![Page 16: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/16.jpg)
Logical Operations
16
3 methods possible:1. Use the logical vector negatives :
sensor is the vector that has the values. It makes sense it comes first, since we are accessing those values.
![Page 17: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/17.jpg)
Logical Operations
17
3 methods possible:1. Use the logical vector negatives :
2. Use the vector whereNegatives as indices.
![Page 18: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/18.jpg)
Logical Operations
18
3 methods possible:3. Use the condition directly (skip the “middleman” variables)
All of these are logical operations.
![Page 19: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/19.jpg)
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
19
![Page 20: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/20.jpg)
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
20
![Page 21: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/21.jpg)
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 vectorresult = x(result2); %use the positionsresult = x(x>=0); %use the condition
result = 34.2 55.5 9.9 21
Let's see examples…
![Page 22: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/22.jpg)
Ex1 – Filtering Data
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.
22
www.allamericanracers.com
http://ceit.uq.edu.au
www.trucktrend.com
AIRFLOW
Static Fluid Equation: ΔP = ρgh±h
![Page 24: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/24.jpg)
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… 24
![Page 25: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/25.jpg)
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… 25
![Page 26: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/26.jpg)
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… 26
![Page 27: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/27.jpg)
Ex1 - Output
27
Logical operation: find()
Logical operation: variableName(positions)
![Page 28: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/28.jpg)
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… 28
Replace by readings from machine!
![Page 29: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/29.jpg)
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
29
![Page 30: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/30.jpg)
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));
30
Use element-wise AND
![Page 31: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/31.jpg)
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
31
Use element-wise AND
![Page 32: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/32.jpg)
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)); 32
Use only 1 symbol for AND and OR.
![Page 33: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/33.jpg)
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));
33
Use element-wise AND
![Page 34: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/34.jpg)
Ex3 – How about matrices?
34
Part Number UnitPrice ($)
101 3.25
65 20.50
290 56.00
450 1.75
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!
![Page 35: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/35.jpg)
Ex3 – Analyze table, cont.
%ask user for part number, and quantitypart_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 .
35
![Page 36: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/36.jpg)
Ex3 – Analyze table, cont.
%ask user for part number, and quantitypart_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 .
36
>> [row, col] = find(table == part_number)row = 2col = 1
![Page 37: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/37.jpg)
Ex3 – Analyze table, cont.
%ask user for part number, and quantitypart_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 priceunit_price = table(row,2);
%calculate sub-total
%add in taxes .
37
![Page 38: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/38.jpg)
Ex3 – Analyze table, cont.
%ask user for part number, and quantitypart_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 priceunit_price = table(row,2);
%calculate sub-totalsub_total = quantity * unit_price;
%add in taxestotal_bill = sub_total * (1+0.65);fprintf('Your bill adds up to: $%20.2f\n', total_bill)
38
![Page 39: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/39.jpg)
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!
39
Part Number UnitPrice ($)
101 3.25
65 20.50
290 65.00
450 1.75
![Page 40: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/40.jpg)
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
>> [row, col] = find(table == part_number)row = 2 3
col = 1 2
Part Number UnitPrice ($)
101 3.25
65 20.50
290 65.00
450 1.75
![Page 41: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/41.jpg)
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 ONLYrow = find(table == part_number);
41
>> [row, col] = find(table == part_number)row = 2 3
col = 1 2
Part Number UnitPrice ($)
101 3.25
65 20.50
290 65.00
450 1.75
That's SLICING again!
(:,1)
![Page 42: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/42.jpg)
Ex4 – Works for strings too!
42
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
![Page 43: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/43.jpg)
Ex4 – Works for strings too!
43
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
![Page 44: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/44.jpg)
Ex4 – Works for strings too!
44
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
![Page 45: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/45.jpg)
Ex4 – Works for strings too!
45
originalDNA =GATACCAT
Or.. In 5 lines!
originalDNA ='GATACCAT'
![Page 46: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/46.jpg)
Ex4 – Works for strings too!
46
originalDNA =GATACCAT
matchDNA = A A
Or.. In 5 lines!
originalDNA ='GATACCAT'
%at the T's location, put an 'A'
matchDNA(originalDNA =='T')='A'
![Page 47: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/47.jpg)
Ex4 – Works for strings too!
47
originalDNA =GATACCAT
matchDNA = A A
matchDNA = TAT TA
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'
![Page 48: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/48.jpg)
Ex4 – Works for strings too!
48
originalDNA =GATACCAT
matchDNA = A A
matchDNA = TAT TA
matchDNA =CTAT TA
matchDNA =CTATGGTA
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'
![Page 49: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/49.jpg)
One last keyword end
It represents automatically the index of the last element in a vector
49
![Page 50: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/50.jpg)
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!)
50
![Page 51: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/51.jpg)
Early File I/OTo help you get started with your final
project
1. Definition of “high level”
2. Is using a High Level function appropriate?
3. xlsread()
4. xlswrite()
5. dlmread()
6. dlmwrite()
51
![Page 52: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/52.jpg)
1. Definition
A file I/O function is considered HIGH-LEVEL if in ONE SINGLE COMMAND, it 1. Opens the file
2. Grabs the data
3. Stores it in variables
4. Closes the file
High-level functions can be used to:1. Load data from external sources (INPUT)
2. Save data back to a file (OUTPUT)
52
![Page 53: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/53.jpg)
2. Wait.wait.wait…
Before you decide if using a high level function is possible, evaluate the following:
1. What type of file is it? (Excel, text, jpg..)
2. Find the organization overall (data in rows, or data in columns, data placed all over the place…)
3. Recognize the delimiters (space, tabs, new lines, -, :, / any specific symbol. What makes it obvious it is a new column? What makes it obvious it is a new row?)
4. Recognize the data types (all numerical? all strings? Or combination of both)
53
![Page 55: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/55.jpg)
2. Can High Level be used? cont.
“Neatly Organized”
1. Rows and Columns are “identifiable”
2. No “holes” anywhere
3. Always the same patterns per line
4. There is no mixes of delimiters (commas, spaces, tabs, dash.. )
55
![Page 56: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/56.jpg)
Questions
Can these files be read using a high level function?
56
![Page 57: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/57.jpg)
Answers
Can these files be read using a high level function?
57
Yes Mixes of – and spaces.
Mixes of , and spaces. Yes. White space only.Decimals are ok.
![Page 58: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/58.jpg)
The input files (.txt, .xls, .xlsx, .m) should all be in the same directory for function calls to work!
58
![Page 60: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/60.jpg)
3. xlsread()
F1, help. The most general form is:
Other calls possible:
60
![Page 61: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/61.jpg)
3. xlsread()
F1, help. The most general form is:
Other calls possible:
61
How do we work with all this?
![Page 62: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/62.jpg)
Example: xlsread()
Reading from a grade book
%load data from file
[values text raw] = xlsread('grades.xlsx');
62
Test. See results!
![Page 63: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/63.jpg)
Example: xlsread()
Reading from a grade book
%load data from file
[values text raw] = xlsread('grades.xlsx');
63
Test. See results!
Note that we are collecting multiple values using an array!
![Page 65: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/65.jpg)
3. xlsread(), cont.
txt =
19 78
22 83
98 99
21 56
23 89
19 51
65
nbs =
'Name' 'Age' 'Grade'
'Fred' '' ''
'joe' '' ''
'SaLLy' '' ''
'CharliE' '' ''
'mary' '' ''
'Ann' '' ''
raw =
'Name' 'Age' 'Grade'
'Fred' [ 19] [ 78]
'joe' [ 22] [ 83]
'SaLLy' [ 98] [ 99]
'CharliE' [ 21] [ 56]
'mary' [ 23] [ 89]
'Ann' [ 19] [ 51]
>> [txt nbs raw] = xlsread('grades.xlsx‘)
Variable names are up to the programmer. If named badly by mistake… BAD.
The order of the return values is important.
![Page 66: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/66.jpg)
3. xlsread(), cont.
Simply omit the 2nd and 3rd return value to collect only numerical values.values = xlsread(‘grades.xlsx’);
66
![Page 67: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/67.jpg)
3. xlsread(), cont.
Simply omit the 2nd and 3rd return value to collect only numerical values.values = xlsread(‘grades.xlsx’);
If a project needs all the data together, collect the 1st and 2nd return values into a dummy variable.[trash trash data] = xlsread(‘grades.xlsx’);
67
![Page 68: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/68.jpg)
3. xlsread(), cont.
Simply omit the 2nd and 3rd return value to collect only numerical values.values = xlsread(‘grades.xlsx’);
If a project needs all the data together, collect the 1st and 2nd return values into a dummy variable.[trash trash data] = xlsread(‘grades.xlsx’);
If there happen to be ‘holes’ in the spreadsheet, MATLAB fills it with a NaN value (not a number). The function isnan() can help determine where those ‘holes’ are.
68
![Page 69: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/69.jpg)
3. xlsread(), cont.
This function will not work under Citrix, since Excel is not installed on Citrix. Come to the lab to do labs and homework.
69
![Page 70: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/70.jpg)
4. xlswrite()
Arrays can also be written to excel sheets:
xlswrite(<filename>, <array>, <sheet>, <range>)
The <sheet> and <range> arguments are optional.
70
clcclear %create phony datatable = rand(5,3)*100; %print to excelxlswrite('testingTesting.xls',table)
![Page 71: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/71.jpg)
2. Can High-Level be used?
71
NUMBERS ONLY.
![Page 72: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/72.jpg)
5. Using Delimiters
Rows are delimited by the new line character (enter key, invisible to the human eye).
Columns are delimited by the same delimiter each time: Default delimiters: commas, and white space Other delimiters: any other symbol : - /
F1 = help
72
![Page 73: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/73.jpg)
5. Using Delimiters, cont.
Delimiter: white space (spaces, or tabs)
73Missing data is filled by zeros.
![Page 74: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/74.jpg)
5. Using Delimiters, cont.
Delimiter: commas Added feasibility to ‘skip’ columns
74
![Page 75: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/75.jpg)
5. Using Delimiters, cont.
Delimiter: other than the defaults
75
Specify the delimiter as the 2nd argument.
![Page 76: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/76.jpg)
5. Using Delimiters, cont.
Delimiter: other than the defaults
76
Specify the delimiter as the 2nd argument.
![Page 77: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/77.jpg)
5. Using Delimiters, cont.
"Neatly Organized" is important.
77
BAD RESULTS.Two delimiters (spaces and colons) in the file. Matlab is lost.
![Page 78: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/78.jpg)
dlmwrite()
M = [2, 123; 87, 4];
dlmwrite('test.txt', M, ',');
File contains:
2,123
87,4
EXCEPT…
78
![Page 79: Logical Ops on Arrays](https://reader036.vdocuments.site/reader036/viewer/2022062519/56814ef4550346895dbc8563/html5/thumbnails/79.jpg)
dlmwrite()
By default, dlmwrite() saves files using Unix line endings – so if you look at the file in Notepad:
This is not “wrong” – just different. This can be changed by setting the “newline” attribute when you call the function:
dlmwrite('test.txt', M, ',', 'newline', 'pc');
79