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

Slides:



Advertisements
Similar presentations
General Computer Science for Engineers CISC 106 Midterm 2 Review James Atlas Computer and Information Sciences 11/06/2009.
Advertisements

Section 2 Appendix Tensor Notation for BP 1. In section 2, BP was introduced with a notation which defined messages and beliefs as functions. This Appendix.
CMPS 1371 Introduction to Computing for Engineers STRUCTURE ARRAYS.
General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009.
Lab2 (Signal & System) Instructor: Anan Osothsilp Date: 07 Feb 07.
General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009.
General Computer Science for Engineers CISC 106 Lecture 24 Christopher Thorpe Computer and Information Sciences 04/17/2009.
General Computer Science for Engineers CISC 106 Final Exam Review Dr. John Cavazos Computer and Information Sciences 05/18/2009.
General Computer Science for Engineers CISC 106 Lecture 25 Dr. John Cavazos Computer and Information Sciences 04/20/2009.
General Computer Science for Engineers CISC 106 Lecture 13 Roger Craig Computer and Information Sciences 3/13/2009.
General Computer Science for Engineers CISC 106 James Atlas Computer and Information Sciences 11/13/2009.
General Computer Science for Engineers CISC 106 James Atlas Computer and Information Sciences 10/30/2009.
General Computer Science for Engineers CISC 106 Lecture 05 Dr. John Cavazos Computer and Information Sciences 2/20/2009.
General Computer Science for Engineers CISC 106 Lecture 23 Dr. John Cavazos Computer and Information Sciences 4/15/2009.
General Computer Science for Engineers CISC 106 James Atlas Computer and Information Sciences 10/26/2009.
General Computer Science for Engineers CISC 106 Lecture 08 James Atlas Computer and Information Sciences 9/21/2009.
General Computer Science for Engineers CISC 106 Lecture 18 Dr. John Cavazos Computer and Information Sciences 3/27/2009.
General Computer Science for Engineers CISC 106 Lecture 06 Roger Craig Computer and Information Sciences 2/23/2009.
BEGINNING MULTIPLICATION BASIC FACTS Multiplication is REPEATED ADDITION. It is a shortcut to skip counting. The first number in the problem tells.
General Computer Science for Engineers CISC 106 Lecture 07 James Atlas Computer and Information Sciences 06/29/2009.
General Computer Science for Engineers CISC 106 Lecture 11 James Atlas Computer and Information Sciences 07/27/2009.
For many calculators if you input "the square root of -1", you will get out "domain error" Input Output This was done with a TI-30XS calculator.
BEGINNING MULTIPLICATION BASIC FACTS Multiplication is REPEATED ADDITION. It is a shortcut to skip counting. The first number in the problem tells.
Computer Science & Engineering 2111 CSE 2111 Lecture NZ Function 1CSE 2111 NZ Function.
General Computer Science for Engineers CISC 106 Lecture 2^4 Roger Craig Computer and Information Sciences 03/23/2009.
Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
General Computer Science for Engineers CISC 106 Lecture 15 Dr. John Cavazos Computer and Information Sciences 03/16/2009.
General Computer Science for Engineers CISC 106 Lecture 14 James Atlas Computer and Information Sciences 08/10/2009.
Improving Matlab Performance CS1114
Chapter 9: Coupling & Cohesion Omar Meqdadi SE 273 Lecture 9 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Computer Science & Engineering 2111 Database Objects 1 CSE 2111 Introduction to Database Management Systems.
General Computer Science for Engineers CISC 106 Lecture 09 Dr. John Cavazos Computer and Information Sciences 03/04/2009.
CISC220 Spring 2010 James Atlas Lecture 04: Pointers, Functions, Memory Management, ADTs, Classes, Hardware, Software, Graphics, Networking, AI, Databases,
INTRODUCTION TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in.
Chapter 5 Ordered List.
Basic Principles Photogrammetry V: Image Convolution & Moving Window:
Network Scale-up method
Arrays: Checkboxes and Textareas
Intro to Programming Week # 6 Repetition Structure Lecture # 10
CITS2401 Computer Analysis & Visualisation
CISC181 Introduction to Computer Science Dr
EGR 115 Introduction to Computing for Engineers
Chapter 4 MATLAB Programming
Introduction to Programming for Mechanical Engineers (ME 319)
Two-Dimensional Plots
Basics of Digital Logic Design Presentation D
Sussex Neuroscience Coding Club title slide
Array Processor.
Resolving collisions: Open addressing
Intro to Nested Looping
Chapter2 Creating Arrays
ME 123 Computer Applications I Lecture 40: Course Review 5/23/03
Communication and Coding Theory Lab(CS491)
Arrays and Matrices in MATLAB
Intro to Nested Looping
CISC181 Introduction to Computer Science Dr
Programming Control Structures with JavaScript Part 2
ARRAY DIVISION Identity matrix Islamic University of Gaza
‘If’ statements, relational operators, and logical operators
Using Script Files and Managing Data
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
USING ARRAYS IN MATLAB BUILT-IN MATH FUNCTIONS
General Computer Science for Engineers CISC 106 Lecture 15
General Computer Science for Engineers CISC 106 Lecture 11
ME 123 Computer Applications I Lecture 4: Vectors and Matrices 3/14/03
ME 123 Computer Applications I Lecture 5: Input and Output 3/17/03
Electrical and Computer Engineering Department SUNY – New Paltz
Presentation transcript:

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

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

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

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

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

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’

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

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

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?

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

Vectorization Additional examples ◦ x = [ ]; ◦ x < 3 ◦ x(x < 3) ◦ x(x 3) x < 3 produces a mask

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

Masking x = [ ]; y = x < 3 x(y) = x(y).* 2;

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

MATLAB functions - any/all x = [ ]; any(x < 3) any(x < 0) all(x > 1) all(x > 0)

MATLAB functions - randi rand() randi(100)

Structures in MATLAB

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

Given: We can implement it with arrays like this: Name Credits Grad 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/ Chris12/15/ Sola05/17/ Roger06/10/ Tom05/22/2012 A Database Application

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

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

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

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.

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