Presentation is loading. Please wait.

Presentation is loading. Please wait.

General Computer Science for Engineers CISC 106 Lecture 10 James Atlas Computer and Information Sciences 07/15/2009.

Similar presentations


Presentation on theme: "General Computer Science for Engineers CISC 106 Lecture 10 James Atlas Computer and Information Sciences 07/15/2009."— Presentation transcript:

1 General Computer Science for Engineers CISC 106 Lecture 10 James Atlas Computer and Information Sciences 07/15/2009

2 Lecture Overview First ◦ MATLAB array initialization  “growing” arrays ◦ Vectorization  mask arrays ◦ MATLAB functions  find  any, all  randi Second ◦ Structures in MATLAB

3 MATLAB Array Initialization y = []; for i = 1:10 y(i) = i; end; How does this work?

4 MATLAB Array Initialization y = []; for i = 1:10 y(i) = i; end; This is an example of “growing” an array

5 MATLAB Array Initialization y = zeros(1,10); for i = 1:10 y(i) = i; end; Initializes the array first

6

7 Vectorization What is vectorization? ◦ Functions that can be performed on the entire array instead of just one element in an array. Advantages of vectorization ◦ Fast and compact. Disadvantages of vectorization ◦ Hard to look at what is going on ‘inside’

8 The ‘loopy’ way ◦ function output = square(input) n = length(input); for i = 1:n output(i) = input(i)^2; end The ‘vector’ way ◦ Output = input.^2; Given an array of arbitrary size: Square each element in the array and put the result into a new array. Vectorization

9 The ‘loopy’ way function count = num_less_than(input, value) n = length(input); count = 0; for i = 1:n if (input(i) < value) count = count + 1; end The ‘vector’ way vector_less = (input < value); count = sum(vector_less); Given an array of arbitrary size: Tell me how many elements of the given array are less than a given value. Vectorization

10 The ‘loopy’ way function count = num_less_than(input, value) n = length(input); count = 0; for i = 1:n if (input(i) < value) count = count + 1; end The ‘vector’ way vector_less = (input < value); count = sum(vector_less); Given an array of arbitrary size: Tell me how many elements of the given array are less than a given value. Vectorization How does the vector way work?

11 Vectorization vector_less = (input < value); count = sum(vector_less); Let’s open MATLAB to see

12 Vectorization Additional examples ◦ x = [1 2 3 4 5 6]; ◦ x < 3 ◦ x(x < 3) ◦ x(x 3) x < 3 produces a mask

13 Masking Masking selects only certain elements of an array to perform an operation Masking uses an array of only 0’s and 1’s that is the same size as the argument ◦ y = x < 3 ◦ whos y ◦ y is a mask of x that selects only the elements that are less than 3

14 Masking x = [1 2 3 4 5 6]; y = x < 3 x(y) = x(y).* 2;

15 MATLAB functions - find find ◦ locates all nonzero elements of array z = [1 2 0 0 0 6]; find(z) ◦ [1 2 6]

16 MATLAB functions - any/all x = [1 2 3 4 5 6]; any(x < 3) any(x < 0) all(x > 1) all(x > 0)

17 MATLAB functions - randi rand() randi(100)

18 Structures in MATLAB

19 A Database Application Given: Name: Chris Credits: 27 Graduation: 12/15/2011 Name: Sola Credits: 18 Graduation: 05/17/2011 Name: Roger Credits: 55 Graduation: 06/10/2009 Name: Tom Credits: 15 Graduation: 05/22/2012

20 Given: We can implement it with arrays like this: Name Credits Grad. 1 2 3 4 Name: Chris Credits: 27 Graduation: 12/15/2011 Name: Sola Credits: 18 Graduation: 05/17/2011 Name: Roger Credits: 55 Graduation: 06/10/2009 Name: Tom Credits: 15 Graduation: 05/22/2012 27Chris12/15/2011 18Sola05/17/2011 55Roger06/10/2009 15Tom05/22/2012 A Database Application

21 Given: OR we can do it like this an array with structs:.d Name: Chris Credits: 27 Graduation: 12/15/2011 Name: Sola Credits: 18 Graduation: 05/17/2011 Name: Roger Credits: 55 Graduation: 06/10/2009 Name: Tom Credits: 15 Graduation: 05/22/2012 Students (1). Name: Chris Students (1).Credits: 27 Students (1). Graduation: 12/15/2011 Students (2).Name: Sola Students (2).Credits: 18 Students (2).Graduation: 05/17/2011 Students (3). Name: Roger Students (3). Credits: 55 Students (3). Graduation: 06/10/2009 Students (4). Name: Tom Students (4). Credits: 15 Students (4). Graduation: 05/22/2012 A Database Application

22 record1.name = 'Me'; record2.name = 'Not Me'; record1.credits = 27; record2.credits = 30; record1.age = 10; record2.age = 14; function [] = displayRecordName(record) disp(record.name); displayRecordName(record1); displayRecordName(record2); Initializing a structure

23 Students = struct{‘name’, {‘Chris’, ‘Sola’,’Roger’, Tom’}, ‘credits’, {27, 18, 55, 15}, ‘graduation’, { ‘12/15/2011’,’ 05/17/2011’,’ 06/10/2009’,’05/22/2012’} Initializing an Array of Structs

24

25 Struct Array Syntax There are multiple ways to create a struct array: s = struct('field1', {}, 'field2', {},...) creates an empty structure with fields field1, field2,... s = struct creates a structure with no fields. s = struct([]) creates an empty structure with no fields.

26 Struct Array Example s = struct('fish',{{'flipper','swimmie'}},'age',{[4], [7]}) s = fish: {'flipper' 'swimmie'} age: [4 7] s(1) ans= fish: 'flipper' age: 4

27


Download ppt "General Computer Science for Engineers CISC 106 Lecture 10 James Atlas Computer and Information Sciences 07/15/2009."

Similar presentations


Ads by Google