Computer Science 121 Scientific Computing Winter 2016 Chapter 4 Collections and Indexing.

Slides:



Advertisements
Similar presentations
Introduction to arrays
Advertisements

Lecture 4.
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Maths for Computer Graphics
Lecture 4 Sept 8 Complete Chapter 3 exercises Chapter 4.
CIS 101: Computer Programming and Problem Solving Lecture 2 Usman Roshan Department of Computer Science NJIT.
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Review Binary –Each digit place is a power of 2 –Any two state phenomenon can encode a binary number –The number of bits (digits) required directly relates.
General Computer Science for Engineers CISC 106 Lecture 08 Dr. John Cavazos Computer and Information Sciences 2/27/2009.
Lecture 7 Sept 19, 11 Goals: two-dimensional arrays (continued) matrix operations circuit analysis using Matlab image processing – simple examples Chapter.
Lecture 6 Sept 15, 09 Goals: two-dimensional arrays matrix operations circuit analysis using Matlab image processing – simple examples.
Concatenation MATLAB lets you construct a new vector by concatenating other vectors: – A = [B C D... X Y Z] where the individual items in the brackets.
JavaScript, Third Edition
Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections.
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
Chapter 11 Section 11.0 Review of Matrices. Matrices A matrix (despite the glamour of the movie) is a collection of numbers arranged in a rectangle or.
Java Unit 9: Arrays Declaring and Processing Arrays.
Computer Science 121 Scientific Computing Winter 2014 Chapter 4 Collections and Indexing.
Algebra 2: Lesson 5 Using Matrices to Organize Data and Solve Problems.
Computer Science 121 Scientific Computing Winter 2014 Chapter 3 Simple Types: Numbers, Text, Booleans.
Chapter 5. Loops are common in most programming languages Plus side: Are very fast (in other languages) & easy to understand Negative side: Require a.
Matlab tutorial course Lesson 2: Arrays and data types
Lists in Python.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
1 Week 3: Vectors and Matrices (Part III) READING: 2.2 – 2.4 EECS Introduction to Computing for the Physical Sciences.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
1 Chapter 3 Arrays (2) 1. Array Referencing 2. Common Operations 1. Slicing 2. Diminution 3. Augmentation 3. List of Commonly Used Built-in Functions 1.
Arrays 1 Multiple values per variable. Why arrays? Can you collect one value from the user? How about two? Twenty? Two hundred? How about… I need to collect.
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.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Logic (continuation) Boolean Logic and Bit Operations.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
2009/9 1 Matrices(§3.8)  A matrix is a rectangular array of objects (usually numbers).  An m  n (“m by n”) matrix has exactly m horizontal rows, and.
ENG College of Engineering Engineering Education Innovation Center 1 Array Accessing and Strings in MATLAB Topics Covered: 1.Array addressing. 2.
Working with Arrays in MATLAB
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Linear Algebra Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
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.
Matrix Algebra Section 7.2. Review of order of matrices 2 rows, 3 columns Order is determined by: (# of rows) x (# of columns)
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
Chapter 1 Section 1.5 Matrix Operations. Matrices A matrix (despite the glamour of the movie) is a collection of numbers arranged in a rectangle or an.
Chapter 14 JavaScript: Part II The Web Warrior Guide to Web Design Technologies.
Array Accessing and Strings ENGR 1187 MATLAB 3. Today's Topics  Array Addressing (indexing)  Vector Addressing (indexing)  Matrix Addressing (indexing)
Table of Contents Matrices - Definition and Notation A matrix is a rectangular array of numbers. Consider the following matrix: Matrix B has 3 rows and.
Computer Science 121 Scientific Computing Winter 2016 Chapter 3 Simple Types: Numbers, Text, Booleans.
To add, subtract, multiply, and divide, absolutely follow the mathematical rules. 1. All the rules 2. The element-per-element operator 3. Ex1: graphing.
Relational and Logical Operators EE 201 1C7-2 Spring 2012.
1-2 What is the Matlab environment? How can you create vectors ? What does the colon : operator do? How does the use of the built-in linspace function.
Chapter 1 Computing Tools Variables, Scalars, and Arrays Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
LAB 2 Vectors and Matrices Dr.Abdel Fattah FARES.
13.4 Product of Two Matrices
ECE 1304 Introduction to Electrical and Computer Engineering
Linear Algebra review (optional)
Matrix Operations.
Warm Up Use scalar multiplication to evaluate the following:
Multiplying Matrices.
Use of Mathematics using Technology (Maltlab)
Matlab tutorial course
Computer Science 210 Computer Organization
CSCI N207 Data Analysis Using Spreadsheet
Data Types and Data Structures
Multiplying Matrices.
Introduction to Matlab
Linear Algebra review (optional)
Multiplying Matrices.
Multiplying Matrices.
Multiplying Matrices.
Introduction to Computer Science
Presentation transcript:

Computer Science 121 Scientific Computing Winter 2016 Chapter 4 Collections and Indexing

We've seen two kinds of collection –Array (sequence of numbers) –Text/string (sequence of characters) Two main issues –How to access individual elements of a collection –How to group related elements together (even when their types differ)

4.1 Indexing Consider census data for a single street: >>> elmstreet = array([3, 5, 2, 0, 4, 5, 1]) Matlab can give us various stats about this data >>> sum(elmstreet) # total residents 20 >>> mean(elmstreet) # mean household size >>> max(elmstreet) # largest household size 5 >>> min(elmstreet) # smallest household size 0

4.1 Indexing Some data may be bogus >>> min(elmstreet) # smallest size 0 Need to know bogus values, and where they “live” In general, need to know –Value of an element –Position (index) of the element

4.1 Indexing: where Boolean operators on arrays give us arrays of Booleans: >>> elmstreet == 0 array([False, False, False, True, False, False, False], dtype=bool) The where operator tells us the indices of the True elements >>> where(elmstreet == 0) (array([3]),) >>> where(elmstreet > 2) (array([0, 1, 2, 4, 5, 6]),) >>> where(elmstreet < 0) (array([], dtype=int64),)

4.1 Indexing: First and last Elements First element has index 0 >>> elmstreet array([3, 5, 2, 0, 4, 5, 1]) >>> elmstreet[0] 3 Last element can be referenced by special -1 index >>> elmstreet[-1] 1

4.1 Indexing: Subsequences Can use a list of indices instead of a single index >>> elmstreet[[0,2,4]] >>> elmstreet[[0,2,4]] = -1 >>> elmstreet array([-1, 5, -1, 0, -1, 5, 1])

4.1 Indexing: Extending an Array Use append to add an element at end of array: >>> elmstreet = append(elmstreet,8) >>> elmstreet elmstreet = Can append more than one element: >>> elmstreet = append(elmstreet,[9,10,11])

Fibonacci Redux With arrays, we only need a single variable and line (versus three) to do Fibonacci: >>> fib = arange(2) >>> fib array([0, 1]) >>> fib = append(fib, fib[-1] + fib[-2]) >>> fib array([0, 1, 1, 2, 3, 5])

4.2: 2D Arrays, a.k.a. Matrices Lots of data are best represented as tables:

4.2 Matrices We can store such data in a matrix: >>> elmstreet = array([[3,2,1,35000],\ [5,2,3,41000],\ [2,1,1,25000],\ [2,2,0,56000],\ [4,2,2,62000],\ [5,3,2,83000],\ [1,1,0,52000]]) Backslash says "continued on next row..." Household index is implicit (as row number)

4.2 Matrices Like len operator for 1D arrays (a.k.a. vectors), shape operator reports size of matrix: >>> shape(elmstreet) (7, 4) With matrices, we use two indices (instead of one) for referencing values: >>> elmstreet[2,3] >>> elmstreet[3,2] 0

4.2 Matrices As with 1D, we can access part of matrix by using an array of indices >>> elmstreet[[3,4,6], 3] array([56000, 62000, 52000]) Grab a whole row using colon notation >>> elmstreet[0,:] # whole first row array([3, 2, 1, 35000])

4.2 Matrices Also works for columns: >>> elmstreet[:, 0] # whole first col array([3, 5, 2, 2, 4, 5, 1])

As with a vector, we can do operations on a scalar and a matrix: >>>elmstreet*2 array([[6,4,2,70000], [10,4,6,82000], [4,2,2,50000], [4,4,0,112000], [8,4,4,124000], [10,6,4,166000], [2,2,0,104000]])

... and element-by-element on two matrices: >>> a = array([[1,2,3],[4,5,6],[7,8,9]]) >>> b = array([[2,4,6],[0,1,0],[0,3,1]]) >>> a + b array([[ 3, 6, 9], [ 4, 6, 6], [ 7, 11, 10]]) >>> a * b array([[ 2, 8, 18], [ 0, 5, 0], [ 0, 24, 9]])

Of course, matrices must be same size: >>> a + array([[1,2],[0,5]]) ValueError: operands could not be broadcast together with shapes (3,3) (2,2) And your socks don’t match either.

We can get a lot of mileage by combining colon and other operations >>> children = elmstreet[:, 2] >>> children array([1, 3, 1, 0, 2, 2, 0]) >>> nokidshouses = where(children == 0) >>> nokidshouses (array([3, 6]),) >>> incomenokids = elmstreet[nokidshouses, 3] >>> incomenokids array([[56000, 52000]]) >>> mean(incomenokids)

Can get rows and cols at same time with where : >>> r,c = where(elmstreet >3) >>> r array([0, 1, 1, 2, 3, 4, 4, 5, 5, 6]) >>> c array([3, 0, 3, 3, 3, 0, 3, 0, 3, 3]) For Boolean ops, use logical_ >>> r,c = where(logical_and(elmstreet >3, elmstreet <= 5)) >>> r array([1,4,5]) >>> c array([0,0,0])

4.3 Dictionaries: Mixed Data Types, with Names as Indices Dictionaries (a.k.a. Data Structures) allow us to put different types of data into the same collection: >>> pt = {} >>> pt["x"] = 3 >>> pt["name"] = "Henry" >>> pt {'x': 3, 'name': 'Henry'}

4.3 Arrays of arbitrary types: Lists >>> friends = ["Sally", "Bob", "Jane"] >>> friends ['Sally', 'Bob', 'Jane'] >>> friends[2] 'Jane' Arrays are great for numerical computing For other types of elements in a sequence, use "bare" lists: Unlike arrays, lists can mix types (not recommended): >>> stuff = [ , "Einstein", arange(5)]