CS112 Scientific Computation Department of Computer Science Wellesley College Vectors and indexing Tools of the trade.

Slides:



Advertisements
Similar presentations
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Advertisements

CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
CS112 Scientific Computation Department of Computer Science Wellesley College Decisions, Decisions… Conditional statements and expressions.
Computer Science 1620 Loops.
1 ICS 101 – LAB 2 Arithmetic Operations I Putu Danu Raharja kfupm.edu.sa Information & Computer Science Department CCSE - King Fahd University.
CS 106 Introduction to Computer Science I 01 / 30 / 2008 Instructor: Michael Eckmann.
Lecture 4 Sept 8 Complete Chapter 3 exercises Chapter 4.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Computer Science 1620 Programming & Problem Solving.
JavaScript, Third Edition
Fall 2006AE6382 Design Computing1 Relational and Logical Operators Use relational operators to test two values Work with values of true and false Compare.
Introduction to C Programming
Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections.
Roots and Fractional Exponents. You know a square root means a number you take times itself to get a given answer.
Loops are MATLAB constructs that permit us to execute a sequence of statements more than once. There are two basic forms of loop constructs: i. while.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Computer Science 1000 Spreadsheets II Permission to redistribute these slides is strictly prohibited without permission.
CS112 Scientific Computation Department of Computer Science Wellesley College Cold Coffee Vectors and plotting.
Chapter 14: Generating Data with Do Loops OBJECTIVES Understand iterative DO loops. Construct a DO loop to perform repetitive calculations Use DO loops.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
CS112 Scientific Computation Department of Computer Science Wellesley College Matrices Storing two-dimensional numerical data.
Estimating Square Roots The square root of a number is the value that, when multiplied by itself, gives the original number. 2 x 2 = 4 Square RootSquare.
CS4311 Spring 2011 Unit Testing Dr. Guoqiang Hu Department of Computer Science UTEP.
CS112 Scientific Computation Department of Computer Science Wellesley College Scientific Computation MATLAB Examples.
Input, Output, and Processing
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
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.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
CS112 Scientific Computation Department of Computer Science Wellesley College Hodgepodge “Clumsy mixture of ingredients…”
PHP Logic. Review: Variables Variables: a symbol or name that stands for a value – Data types ( Similar to C++ or Java): Int, Float, Boolean, String,
CPS120: Introduction to Computer Science Operations Lecture 9.
9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
CS112 Scientific Computation Department of Computer Science Wellesley College Building your own Functions.
ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot.
CS 100Lecture 21 CS100J: Lecture 2 n Previous Lecture –Programming Concepts n problem, algorithm, program, computer, input, output, sequential execution,
CS112 Scientific Computation Department of Computer Science Wellesley College Scientific Computation What’s the big idea?
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.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
CS112 Scientific Computation Department of Computer Science Wellesley College AI designs a forest Decision trees.
Variables 1. What is a variable? Something whose value can change over time This value can change more than once over the time period (no limit!) Example:
Chapter 7 Problem Solving with Loops
CS112 Scientific Computation Department of Computer Science Wellesley College Tables More matrix fun.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
CS112 Scientific Computation Department of Computer Science Wellesley College Decisions, Decisions… Conditional statements and expressions.
CS112 Scientific Computation Department of Computer Science Wellesley College Storing Values for Safe Keeping Variables and their Values.
CS112 Scientific Computation Department of Computer Science Wellesley College Building your own Functions.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.
Repetition Looping. Types for while do..while for The for loop allows you to iterate through a variable for a specific range of values. You must supply.
 Scientific notation is simply a method for expressing, and working with, very large or very small numbers. It is a short hand method for writing numbers,
Bitwise Operations C includes operators that permit working with the bit-level representation of a value. You can: - shift the bits of a value to the left.
REPETITION CONTROL STRUCTURE
Upsorn Praphamontripong CS 1110 Introduction to Programming Fall 2016
Chapter 5 - Control Structures: Part 2
Prime Numbers.
Counted Loops.
MATLAB: Structures and File I/O
11/10/2018.
Linear Equations Lesson 2.3.
Iteration: Beyond the Basic PERFORM
CSCI N207 Data Analysis Using Spreadsheet
Objective The student will be able to:
Computing Fundamentals
CS 1111 Introduction to Programming Spring 2019
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.
Basic Concepts of Algorithm
Mathematical Analysis of Algorithms
Presentation transcript:

CS112 Scientific Computation Department of Computer Science Wellesley College Vectors and indexing Tools of the trade

Indexing5-2 Analyzing health data What is the average cholesterol level for women in their twenties who exercise at least 30 minutes a day? 189 cholesterol age sex exercise ‘m’ ‘f’‘m’ ‘f’

Indexing5-3 The length of the sunspot cycle: ~ 12 years Note: virtually no sunspot activity in the years (Europe’s mini ice-age) and we’re currently in a low spot in the cycle!

Indexing5-4 Indexing Each location of a vector stores a value and has an index that specifies its position within the vector numbers = [ ] The first location has index 1, and indices increment by 1 for successive locations 3 numbers

Indexing5-5 Reading, Riting, & Rithmetic We can read or change the contents of a location by referring to its index num = numbers(2) numbers(5) = 4 sumNum = numbers(2) + numbers(5) 3 numbers numsumNum 4

Indexing5-6 The end game The keyword end, when provided as an index of a vector, refers to the last index of the vector sumNum = numbers(2) + numbers(5) sumNum = numbers(2) + numbers(end) Exercise: Write a sequence of assignment statements that exchange the contents of the first and last locations of numbers, assuming you don’t know the length of numbers 3 numbers

Indexing5-7 Out of bounds An attempt to read the contents of a location whose index is outside the range of indices is not good >> numbers = [ ]; >> num = numbers(8) ??? Index exceeds matrix dimensions. >> num = numbers(end + 1) ??? Index exceeds matrix dimensions. 3 numbers

Indexing5-8 MATLAB is more forgiving than the emperor However, MATLAB allows you to add a new value at a location beyond the range of indices >> numbers = [ ]; >> numbers(6)= 9; >> numbers(9) = 4; 3 numbers

Indexing5-9 Referring to multiple locations We can refer to multiple locations all at once using a vector of indices >> newNumbers = numbers( [2 4] ) 7 newNumbers * What is the value of newNumbers after executing: newNumbers = numbers( [ ] ) ? 3 numbers

Indexing5-10 Change contents of multiple locations We can also change the values stored in multiple locations using a vector of indices >> numbers( [ ] ) = 2 3 numbers

Indexing5-11 Different locations get different values Change multiple locations all at once, given equal length indices on the left & values on the right >> numbers( [2 4 6] ) = [9 5 1] 3 numbers

Indexing5-12 Time-out exercises Given the numbers vector, what will be the new contents of numbers after executing the following statements? >> numbers( [4 5] ) = numbers(8) >> numbers( [1 2 3] ) = numbers( [7 end 6] ) Rewrite the following statement by referring to the two indices 1 and 6 in a vector [1 6]: sumNum = numbers(1) + numbers(6) 3 numbers

Indexing5-13 Selecting vector contents with logical vectors Suppose we want to refer to vector locations whose contents satisfy a logical condition* * Remember our health question: what is the average cholesterol level for women in their twenties who exercise at least 30 minutes a day? For example: (1) Change all negative numbers to 0 (2) Store the even and odd numbers in separate vectors (3) Calculate the average of all numbers larger than 10

Indexing5-14 Selection using logical vectors A logical vector, when supplied as an index of a vector, selects locations where logical value is 1 (true) Case 1: Logical vector is given as the index of a vector in an expression negNums = nums(nums < 0) 6 nums negNums

Indexing5-15 Selection using logical vectors (again) Case 2: Logical vector is given as the index of a vector on the left side of an assignment nums(nums < 0) = 0 6 nums nums * This was task number (1)

Indexing5-16 Selection using logical vectors (again) 2 Store even and odd numbers in separate vectors* evenNums = oddNums = 8 nums * Hint: rem(a,b) returns the remainder of dividing a by b

Indexing5-17 Selection using logical vectors (again) 3 Calculate the average of all numbers larger than 10* avgVal = 8 nums * Hint: The mean() function is your friend

Indexing5-18 Now for the pièce de résistance What is the average cholesterol level for women in their twenties who exercise at least 30 minutes a day? 189 cholesterol age sex exercise ‘m’ ‘f’‘m’ ‘f’