Indexing Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Slides:



Advertisements
Similar presentations
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Advertisements

Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Conditional Execution
Introduction to arrays
Arrays, A1 COMP 401, Fall 2014 Lecture 4 8/28/2014.
Slicing Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Copyright © 2014 Dr. James D. Palmer; This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Tuples Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
An introduction to arrays
Software Engineering Fundamental data types. Guidelines - numbers Avoid “magic numbers” (hard- coded values that are not self- explanatory): Changes can.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Maths for Computer Graphics
Binary Search Visualization i j.
Lecture 4 Sept 8 Complete Chapter 3 exercises Chapter 4.
Chapter 7 Matrix Mathematics Matrix Operations Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Introduction Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
Java Unit 9: Arrays Declaring and Processing Arrays.
Recommendations Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Chapter 4 MATLAB Programming Combining Loops and Logic Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Mathcad Variable Names A string of characters (including numbers and some “special” characters (e.g. #, %, _, and a few more) Cannot start with a number.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Class 3 Programming in Visual Basic. Class Objectives Learn about input/output Learn about strings Learn about subroutines Learn about arrays Learn about.
Introduction to Python By Neil Cook Twitter: njcuk Slides/Notes:
Basic File Input and Output Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
© 2000 Scott S Albert Selection Structures Structured Programming 256 Chapter 4.
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.
Linear Algebra Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Python Arrays. An array is a variable that stores a collection of things, like a list. For example a list of peoples names. We can access the different.
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Linear Algebra Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Introduction Copyright © Software Carpentry This work is licensed under the Creative Commons Attribution License See
Basic Flow Control Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
Section 4Chapter 4. 1 Copyright © 2012, 2008, 2004 Pearson Education, Inc. Objectives Solving Systems of Linear Equations by Matrix Methods Define.
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.
Slide Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley A set of equations is called a system of equations. The solution.
Basics Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Lists Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
A: A: double “4” A: “34” 4.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 How did your team meetings go? A list of three possible “adoptable” projects is linked near the bottom.
Programming Languages Programming languages are a compromise between spoken language and formal math. They allow humans to communicate with computers at.
11 PART 2 ARRAYS. 22 PROCESSING ARRAY ELEMENTS Reassigning Array Reference Variables The third statement in the segment below copies the address stored.
Basics Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Strings Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Computer Science 121 Scientific Computing Winter 2016 Chapter 4 Collections and Indexing.
1 ENERGY 211 / CME 211 Lecture 7 October 6, 2008.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Relational and Logical Operators EE 201 1C7-2 Spring 2012.
Python Aliasing Copyright © Software Carpentry 2010
Number and patterns quiz
Python NumPy AILab Batselem Jagvaral 2016 March.
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Boolean Bingo!.
LabVIEW.
MATLAB Programming Indexing Copyright © Software Carpentry 2011
MATLAB Programming Basics Copyright © Software Carpentry 2011
Topics Basic String Operations String Slicing
Dr. Sampath Jayarathna Cal Poly Pomona
Program Design Invasion Percolation: The Grid
Dr. Sampath Jayarathna Old Dominion University
Topics Basic String Operations String Slicing
Topics Basic String Operations String Slicing
Presentation transcript:

Indexing Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See for more information. Matrix Programming

MatricesIndexing Can slice arrays like lists or strings >>> block array([[ 10, 20, 30, 40], [110, 120, 130, 140], [210, 220, 230, 240]]) >>> block[0:3, 0:2] array([[ 10, 20], [110, 120], [210, 220]]) : 2 0:3

MatricesIndexing Can slice arrays like lists or strings >>> block array([[ 10, 20, 30, 40], [110, 120, 130, 140], [210, 220, 230, 240]]) >>> block[0:3, 0:2] array([[ 10, 20], [110, 120], [210, 220]]) Are slices aliases or copies? : 2 0:3

MatricesIndexing Can assign to slices >>> block[1, 1:3] = 0 >>> block array([[ 10, 20, 30, 40], [110, 0, 0, 140], [210, 220, 230, 240]])

MatricesIndexing Slice on both sides to shift data >>> vector = array([10, 20, 30, 40]) >>> vector[0:3] = vector[1:4] >>> vector array([20, 30, 40, 40]) Not overwritten

MatricesIndexing Slice on both sides to shift data >>> vector = array([10, 20, 30, 40]) >>> vector[0:3] = vector[1:4] >>> vector array([20, 30, 40, 40]) Is this easier to understand than a loop? Not overwritten

MatricesIndexing Can use lists or arrays as subscripts >>> vector array([0, 10, 20, 30]) >>> subscript = [3, 1, 2] >>> vector[ subscript ] array([30, 10, 20]) vector subscript result

MatricesIndexing Also works in multiple dimensions Though operation may not be obvious >>> square = numpy.array([[5, 6], [7, 8]]) >>> square[ [1] ] array([[7, 8]])

MatricesIndexing Also works in multiple dimensions Though operation may not be obvious >>> square = numpy.array([[5, 6], [7, 8]]) >>> square[ [1] ] array([[7, 8]]) Did we mention NumPy's excellent documentation?

MatricesIndexing Also works in multiple dimensions Though operation may not be obvious >>> square = numpy.array([[5, 6], [7, 8]]) >>> square[ [1] ] array([[7, 8]]) LOOPS

MatricesIndexing Comparisons >>> vector array([0, 10, 20, 30]) >>> vector < 25 array([ True, True, False, False ], dtype=bool) Data type is Boolean

MatricesIndexing Use a Boolean subscript as a mask >>> vector array([0, 10, 20, 30]) >>> vector[ vector < 25 ] array([0, 10, 20]) vecto r T T T F vector < resul t

MatricesIndexing Use a Boolean subscript as a mask >>> vector array([0, 10, 20, 30]) >>> vector[ vector < 25 ] array([0, 10, 20]) vecto r T T T F vector < resul t Copy or alias?

MatricesIndexing Use masking for assignment >>> a = array([0, 1, 2, 3]) >>> mask = array([True, False, True, False]) >>> a[mask] = array([100, 101, 102, 103]) >>> a array([100, 1, 101, 3]) T F T F mask Taken in order a T F T F maskfill result

MatricesIndexing The putmask function works slightly differently >>> a = array([0, 1, 2, 3]) >>> putmask(a, mask, array([100, 101, 102, 103])) >>> a array([100, 1, 102, 3]) a T F T F mask fill result Taken where True

MatricesIndexing Python does not allow objects to re-define the meaning of and/or/not >>> vector = array([0, 10, 20, 30]) >>> vector <= 20 array([True, True, True, False], dtype=bool) >>> (vector = 20) ValueError: The truth value of an array with more than one element is ambiguous.

MatricesIndexing Use logical_and / logical_or functions >>> logical_and(vector = 20) array([False, False, True, False], dtype=bool) Or | for or, & for and >>> (vector = 20) array([False, False, True, False], dtype=bool)

MatricesIndexing The operators | and & deserve another look: >>> a = n.array([ 1, 2 ]) >>> b = n.array([ 1, -1 ]) >>> a | b array([ 1, -1 ]) >>> a & b array([ 1, 2 ]) logical_and/logical_or treat nonzero as True

MatricesIndexing Use where instead of if/else >>> vector = array([10, 20, 30, 40]) >>> where(vector < 25, vector, 0) array([10, 20, 0, 0]) >>> where(vector > 25, vector/10, vector) array([10, 20, 3, 4])

MatricesIndexing Use where instead of if/else >>> vector = array([10, 20, 30, 40]) >>> where(vector < 25, vector, 0) array([10, 20, 0, 0]) >>> where(vector > 25, vector/10, vector) array([10, 20, 3, 4]) What do choose and select do?

MatricesIndexing Review: –Arrays can be sliced –Or subscripted with vectors of indices –Or masked with conditionals

MatricesIndexing Review: –Arrays can be sliced –Or subscripted with vectors of indices –Or masked with conditionals LOOPS

November 2010 created by Richard T. Guy Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See for more information.