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

Slides:



Advertisements
Similar presentations
Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
Advertisements

General Computer Science for Engineers CISC 106 Midterm 2 Review James Atlas Computer and Information Sciences 11/06/2009.
Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort.
CIS 101: Computer Programming and Problem Solving Lecture 7 Usman Roshan Department of Computer Science NJIT.
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 James Atlas Computer and Information Sciences 10/23/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 08 Dr. John Cavazos Computer and Information Sciences 2/27/2009.
General Computer Science for Engineers CISC 106 Lecture 08
General Computer Science for Engineers CISC 106 Lecture 13 Roger Craig Computer and Information Sciences 3/13/2009.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
General Computer Science for Engineers CISC 106 James Atlas Computer and Information Sciences 11/13/2009.
General Computer Science for Engineers CISC 106 Lecture 09 James Atlas Computer and Information Sciences 9/25/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 James Atlas Computer and Information Sciences 10/26/2009.
Introduction to MATLAB MECH 300H Spring Starting of MATLAB.
EPSII 59:006 Spring Topics Using TextPad If Statements Relational Operators Nested If Statements Else and Elseif Clauses Logical Functions For Loops.
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling.
General Computer Science for Engineers CISC 106 Lecture 07 James Atlas Computer and Information Sciences 06/29/2009.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
ELG 3120 Signal and System Analysis 1 Introduction to MATLAB TAs Wei Zhang Ozgur Ekici (Section A)(Section B) ELG 3120 Lab Tutorial 1.
GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.
General Computer Science for Engineers CISC 106 Lecture 11 James Atlas Computer and Information Sciences 07/27/2009.
Matlab Programming, part 1 M-files It is generally more convenient to program in Matlab using m-files, ascii text files containing a set of Matlab commands.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
COMP 116: Introduction to Scientific Programming Lecture 28: Midterm #2 Review.
CSC Intro. to Computing Lecture 13: PALGO. Announcements Midterm is in one week  Time to start reviewing  We will do more review in class Tuesday.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Module 4 Matlab ME 6104 – Fundamentals of Computer-Aided Design.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
ENG College of Engineering Engineering Education Innovation Center 1 Basic For Loops in MATLAB Programming in MATLAB / Chapter 6 Topics Covered:
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Digital Image Processing Introduction to M-function Programming.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
General Computer Science for Engineers CISC 106 Lecture 2^4 Roger Craig Computer and Information Sciences 03/23/2009.
General Computer Science for Engineers CISC 106 Lecture 03 James Atlas Computer and Information Sciences 2/17/2010.
Digital Image Processing Introduction to MATLAB. Background on MATLAB (Definition) MATLAB is a high-performance language for technical computing. The.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.
General Computer Science for Engineers CISC 106 Lecture 15 Dr. John Cavazos Computer and Information Sciences 03/16/2009.
Introduction to Matlab Module #10 Page 1 Introduction to Matlab Module #10 – Creating Graphical User Interfaces Topics 1.Overview of GUI Development using.
Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
SCRIPTS AND FUNCTIONS DAVID COOPER SUMMER Extensions MATLAB has two main extension types.m for functions and scripts and.mat for variable save files.
General Computer Science for Engineers CISC 106 Lecture 06 James Atlas Computer and Information Sciences 06/24/2009.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
General Computer Science for Engineers CISC 106 Lecture 03 James Atlas Computer and Information Sciences 6/15/2009.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Chapter 16: Searching, Sorting, and the vector Type.
General Computer Science for Engineers CISC 106 Lecture 03 James Atlas Computer and Information Sciences 9/9/2009.
General Computer Science for Engineers CISC 106 Lecture 09 Dr. John Cavazos Computer and Information Sciences 03/04/2009.
General Computer Science for Engineers CISC 106 Lecture 10 James Atlas Computer and Information Sciences 07/15/2009.
Matlab Training Session 4: Control, Flow and Functions
Scripts & Functions Scripts and functions are contained in .m-files
More on Graphical User Interfaces
Matlab Workshop 9/22/2018.
StatLab Matlab Workshop
Use of Mathematics using Technology (Maltlab)
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Presentation transcript:

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

Lecture Overview What if I want to do more Computer Science? ◦ Other courses ◦ Minor in CIS Final Review - Comprehensive

Lecture Overview Final Review - Comprehensive ◦ Midterm 1  Unix commands  if statements  arrays (or matrices)  loops  scripts  functions ◦ Midterm 2  recursion  search  sorting  vectorization  MATLAB functions - find/any/all/rand  structures ◦ Classes ◦ Cell Arrays ◦ Graphics ◦ Function callbacks (passing functions to functions)

Unix Commands When you log into a UNIX terminal ◦ You are in your home directory. ◦ To see the files in your directory.  ls ◦ To make an new folder/directory.  mkdir exampledir ◦ To change directories.  cd exampledir ◦ To go back one directory.  cd.. ◦ To go back to your home directory.  cd

Basic if statements IF statements allow program to make choices whether a condition is met or not if (expression1) statements1; end if (expression2) statements2; end

IF/Elseif Statements if (expression1) statements1; elseif (expression2) statements2; else statements3; end

Major Relational Operators ◦ A < B A is less than B ◦ A > B A is greater than B ◦ A <= B A is less than or equal to B ◦ A >= B A is greater than or equal to B ◦ A == B A is equal to B ◦ A ~= B A not equal B

If statements print “blue” if N <= 5 print “red” if N > 5 and N <= 10 print “green” if N > 10

If statements (cont’d) if (N <= 5) fprintf('blue\n‘); end if (N > 5 & N <= 10) fprintf('red\n‘); end if (N > 10) fprintf('green\n‘); end

Arrays (aka matrices) All variables in matlab are arrays An array of one element is called a scalar A one dimension array is called a vector x=3.14;  scalar a = [1,2,3,4,5];  vector

Arrays (aka matrices) x = 1:0.5:5 Now x is an array of numbers; x = [1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0]

Arrays (aka matrices) A = [1, 2; 3, 4; 5, 6] Creates a 3x2 array, 3 rows, 2 columns. semicolon creates a new row. A =

For Loops Used when you know how many times code is to be executed. Syntax for = : : Variable is initially the start value At end of iteration variable changes by increment If value is not greater than end the loop runs again.

Example Problem I want to find the average # of widgets sold in 4 days Day# of widgets sold Widget(1) = 15 Widget(2) = 22 Widget(3) = 20 Widget(4) = 18 Avg = (Widget(1) + Widget(2) + Widget(3) + Widget(4)) / 4 ◦ This is easy for a small number of days. ◦ What if we had a 1000 days? ◦ We can use a for loop!

Example Problem total = 0; for i = 1:1:1000 loop starts at 1 total = total+widget (i);loop increments by 1 end loop ends at 1000 avg = total / 1000;

A Loop Analogy (for) The runner executes a loop. If they know the distance they want to run For loop for lapCount = start : 1 : end runLap() end

A Loop Analogy (while) The runner executes a loop. If they don’t know the distance they want to run (run until tired) While loop tired = false; while(~tired) tired = runLap() end

Scripts files Store commands in Variables are global, available after you call script file

Scripts files sumIt=0; for current=1:finish if (mod(current,2)==1) sumIt=sumIt+current; end

Functions Special type of m-file ◦ Function name same as file name Contains a function name, arguments, output, and “implementation” All variables in function are local ◦ They are not visible outside call!

Example Function function sumIt=sumOddInt(finish) sumIt=0; for current=1:finish if (mod(current,2)==1) sumIt=sumIt+current; end % sumIt, current, and finish are local

When you call a function… function foo1 function bar1 function foo2 function bar2 function main function main calls foo1 function foo1 calls bar1 function bar1 calls foo2 function foo2 calls bar2 function bar2 executing

Recursion Example Classic Example ◦ Function output = numbersSum(input) if (input == 1) output = 1; else output = input+numbersSum(input-1) end

Linear Search - Iterative Given [ ] find which position 6 occupies Alternatively, does the array contain the number 6?

Linear Search - Recursive Split array in half Search each half of array

Binary Search - Recursive Find N in list Pick a number X halfway between the start and end of the list If X == N we are done else if X < N search top half of list else search bottom half of list

Selection Sort 1. Find the minimum value in the list 2. Swap it with the value in the first position 3. Repeat the steps above for the remainder of the list Starting at the second position and advancing each time

Merge Sort 1. If the list is of length 0 or 1, then it is already sorted. 2. Otherwise: 1.Divide the unsorted list into two sublists of about half the size. 2.Sort each sublist recursively by re-applying merge sort. 3.Merge the two sublists back into one sorted list.

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

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’

Vectorization Examples ◦ x = [ ]; ◦ x < 3 ◦ x(x < 3) ◦ x(x 3) ◦ vector_less = (x < 3); count = sum(vector_less); 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

Classes Object-oriented Programming Classes represent types of Objects Similar to structs

Class definition classdef dog properties name age end

Class definition (cont’) classdef dog properties name age end methods function obj=dog(name, age) obj.name = name; obj.age = age; end

The isa function isa(object, ‘classname’) isa(dog1, ‘dog’) isa(dog1, ‘person’)

Arrays of classes? [dog1 dog2] [dog1 dog2 person1] ?

Cell Arrays Normal array: zeros(1,5) = 5 doubles [ ] = 4 doubles Cell array: { } = ???

Cell Arrays (cont) c = { } c{1} = ‘hello’ Cell arrays store an object reference ◦ Can be another array ◦ Or a string ◦... any struct/object/cell array

MATLAB Graphics Event-driven user interface A figure window contains: ◦ axes, line, plot, image, and figure itself ◦ controls such as buttons, menus, listboxes, textboxes  use uicontrol, and uimenu objects

MATLAB Simple GUI function simpleGUI x = 1; mainFig = figure('Visible','on',... 'name','SimpleGui',... 'Position',[210,170,660,440]); PlotButton = uicontrol('Style','PushButton',... 'Position',[200,43,80,40],... 'String','Click',... function func_Callback(source,eventdata) disp('button clicked'); disp(x); x = x + 1; end

Function callbacks Passing a function to another function function []=functionPassTest(func, data) func(data) function []=printName(obj) disp(obj.name) cat.name = ‘Fifi’ cat);