Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 Lecture Overview MATLAB array initialization ◦ “growing” arrays Vectorization ◦ mask arrays MATLAB functions ◦ find ◦ any, all ◦ randi

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)


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

Similar presentations


Ads by Google