Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections.

Slides:



Advertisements
Similar presentations
Formulas, Ranges, and Functions. Formulas n Formulas perform operations such as addition, multiplication, and comparison on worksheet values. n Formulas.
Advertisements

Introduction to arrays
Introduction to Matlab
A MATLAB function is a special type of M-file that runs in its own independent workspace. It receives input data through an input argument list, and returns.
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 3.
Arrays  An array is a collection of like elements.  There are many engineering applications that use arrays.  MATLAB ® stores data in arrays and performs.
Lecture 4 Sept 8 Complete Chapter 3 exercises Chapter 4.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
CIS 101: Computer Programming and Problem Solving Lecture 2 Usman Roshan Department of Computer Science NJIT.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
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 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Chapter 10 Review: Matrix Algebra
Chapter 5. Loops are common in most programming languages Plus side: Are very fast (in other languages) & easy to understand Negative side: Require a.
1 Week 12 Arrays, vectors, matrices and cubes. Introduction to Scientific & Engineering Computing 2 Array subscript expressions n Each subscript in an.
Matlab tutorial course Lesson 2: Arrays and data types
Martin Ellison University of Warwick and CEPR Bank of England, December 2005 Introduction to MATLAB.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.
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.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
CSE123 Lecture 5 Arrays and Array Operations. Definitions Scalars: Variables that represent single numbers. Note that complex numbers are also scalars,
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.
Representing numbers and Basic MATLAB 1. Representing numbers  numbers used by computers do not behave the same as numbers used in mathematics  e.g.,
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
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.
1 Lab 2 of COMP 319 Lab tutor : Shenghua ZHONG Lab 2: Sep. 28, 2011 Data and File in Matlab.
I Power Int 2 Computing Software Development High Level Language Constructs.
Recap Script M-file Editor/Debugger Window Cell Mode Chapter 3 “Built in MATLAB Function” Using Built-in Functions Using the HELP Feature Window HELP.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Covenant College November 14, Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 3: Vectors and Arrays.
Working with Arrays in MATLAB
A string is an array of characters Strings have many uses in MATLAB Display text output Specify formatting for plots Input arguments for some functions.
I Power Higher Computing Software Development High Level Language Constructs.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
>> x = [ ]; y = 2*x y = Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions.
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.
(The Transpose Operator) 1 >> C=[ ; ; ] C = >> D=C' D =
Array Operations ENGR 1181 MATLAB 4.
Programming with Matlab Day 2: More about Loops, Vectors and Matrices.
Array Creation ENGR 1187 MATLAB 2. Today’s Topics  Arrays: What are they?  Vectors  Matrices  Creating Arrays.
1 Lecture 3 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
CMPS 1371 Introduction to Computing for Engineers VECTORS.
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
CS100A, Fall 1998, Lecture 191 CS100A, Fall 1998 Lecture 19, Thursday Nov 05 Matlab Concepts: Matlab arrays Matlab subscripting Matlab plotting.
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
Chapter 1 Computing Tools Variables, Scalars, and Arrays Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
A rectangular array of numeric or algebraic quantities subject to mathematical operations. The regular formation of elements into columns and rows.
Chapter 9: Data types and data structures OCR Computing for A Level © Hodder Education 2009.
Some “What’s the output” questions to get the day started… >>A = [1 2 3; 3 5 6] This statement stores the matrix: 1. A= 2. A= 3. A= 4. A= Ask Garvin’s.
Numeric, Cell and Structural Arrays One of the strenghts of MATLAB is the capabilty to handle collection of numbers called ARRAYS. MATLAB refers to scalars,
L – Modeling and Simulating Social Systems with MATLAB
Chapter 3 Arrays and Vectors
Use of Mathematics using Technology (Maltlab)
Vectorized Code, Logical Indexing
MATLAB Programming Indexing Copyright © Software Carpentry 2011
Loop Statements & Vectorizing Code
Arrays in Matlab UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Loop Statements & Vectorizing Code
Presentation transcript:

Lecture 4 Sept 7 Chapter 4

Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections of numbers in the form of vectors and arrays. For each of these collections, you will learn how to: ■ Create them ■ Manipulate them ■ Access their elements ■ Perform mathematical and logical operations on them

Concept: Data Collections This section considers two very common ways to group data: in arrays and in vectors. Data Abstraction allows us to refer to groups of data collectively: –“all the temperature readings for May” or –“all the purchases from Wal-Mart.” We can not only move these items around as a group, but also perform mathematical or logical operations on these groups, e.g.: –compute the average, maximum, or minimum temperatures for a month A Homogeneous Collection is constrained to accept only items of the same data type – in this case, they will all be numbers

MATLAB Vectors Individual items in a vector are usually referred to as its elements. Vector elements have two separate and distinct attributes that make them unique in a specific vector: –their numerical value and –their position in that vector. For example, the individual number 66 is the third element in this vector. Its value is 66 and its index is 3. There may be other items in the vector with the value of 66, but no other item will be located in this vector at position 3.

Vector Manipulation We consider the following basic operations on vectors: –Creating a Vector –Determining the size of a Vector –Extracting data from a vector by indexing –Shortening or expanding a Vector –Mathematical and logical operations on Vectors Support for vector processing is a distinct feature of Matlab and it is where it differs significantly from languages like c++ and Java.

Creating a Vector Entering the values directly, e.g. A = [2, 5, 7, 1, 3] Entering the values as a range of numbers e.g., B = 1:3:20 Using the linspace(...) function e.g. c= linspace (0, 20, 11) Using the functions zeros(1,n), ones(1,n), rand(1,n) and randn(1,n) to create vectors filled with 0, 1, or random values between 0 and 1

Problem: Write a code segment in Matlab to generate a sequence containing the outcomes of 50 independent rolls of a fair die. We need to generate a vector of size(100) where each element of the vector is a random member of the set {1, 2, 3, 4, 5, 6}. Need a Matlab function to generate random numbers. rand() gives a random real number between 0 and 1.

Later we will write code to count how many times the outcome was j for different values of j = 1, 2, …, 6. If the random number generator is a good one, we expect all these numbers to be close to each other (when the number N of rolls is large.)

Size of vectors and arrays MATLAB provides two functions to determine the size of arrays in general (a vector is an array with one row): –the function size(A) when applied to the array A returns vector containing two quantities: the number of rows and the number of columns –The function length(A) returns the maximum value in the size of an array; for a vector, this is its length.

Indexing a Vector The process of extracting values from a vector, or inserting values into a vector Syntax: –v(index) returns the element(s) at the location(s) specified by the vector index. –v(index) = value replaces the elements at the location(s) specified by the vector index. The indexing vector may contain either numerical or logical values

Exercise: Write a one-line statement to compute the average of a set of numbers stored in vector x.

Answer: >> sum(x) / length(x)

Numerical Indexing The indexing vector may be of any length It should contain integer (non-fractional) numbers The values in the indexing vector are constrained by the following rules: –For reading elements, all index values j must be 1 <= j <= length (vector) –For replacing elements, all index values j must be j <= element

Replacement Rules 1.Either: All dimensions of the blocks on either side of the replacement instruction must be equal, or There must be a single element on the RHS of the replacement 2.If you replace beyond the end of the existing vector, the vector length is automatically increased. Any element not specifically replaced remains unchanged. Elements beyond the existing length not replaced are set to 0.

Logical Indexing The indexing vector length must be less than or equal to the original vector length It must contain logical values (true or false) Access to the vector elements is by their relative position in the logical vector –When reading elements, only the elements corresponding to true index values are returned –When replacing elements, the elements corresponding to true index values are replaced Logical vectors in Matlab echo in the Command window as 1 or 0, they represent T and F respectively.

Exercise: Let u be a vector. Write a Matlab code segment that determines the number of occurrences of 7 in it. Example: >> u = [ ]; >> >> ans = 2

Exercise: Let u be a vector. Write a Matlab code segment that determines the number of occurrences of 7 in it. Example: >> u = [ ]; >> >> ans = 2 Answer: u(u == 7)/7

Find function

Exercise: Write a program in Matlab to split a vector u into two vectors v and w where v contains all the numbers up to the largest number and w contains the rest. Example: >> u = [ ] >> >> v ans = >> w ans =

Exercise: Write a program in Matlab to split a vector u into two vectors v and w where v contains all the numbers up to the largest number and w contains the rest. Example: >> u = [ ] >> v = u(1:find(u == max(u))) >> v ans =

Exercise: Write a Matlab expression to determine if there are any negative numbers in a vector u. Answer:

Exercise: Write a Matlab expression to determine if there are any negative numbers in a vector u. Answer: >> u = [2, -4, 5, -3, 8, 11]; >> any(u < 0) ans = 1 You can count the number of negative numbers in a vector using sum(u < 0).

Operating on Vectors Three techniques extend directly from operations on scalar values: ■ Arithmetic operations ■ Logical operations ■ Applying library functions Two techniques are unique to arrays in general, and to vectors in particular: ■ Concatenation ■ Slicing (generalized indexing)

Arithmetic operations Arithmetic operations: >> A = [ ]; >> A + 5 ans = >> A.* 2 ans = >> B = -1:1:3 B =

Arithmetic operations (continued) >> A.* B % element-by-element multiplication ans = >> A * B % matrix multiplication!! ??? Error using ==> mtimes Inner matrix dimensions must agree. >> C = [1 2 3] C = >> A.* C % A and C must have the same length ??? Error using ==> times Matrix dimensions must agree.

Logical operations >> A = [ ]; >> B = [ ]; >> A >= 5 ans = >> A >= B ans = >> C = [1 2 3]; >> A > C ??? Error using ==> gt Matrix dimensions must agree.

Logical operations (continued) >> A = [true true false false]; >> B = [true false true false]; >> A & B ans = >> A | B ans = >> C = [1 0 0]; % NOT a logical vector >> A(C) % yes, you can index logical vectors, but... ??? Subscript indices must either be real positive integers or logical.

Applying library functions All MATLAB functions accept vectors of numbers rather than single values and return a vector of the same length. Special Functions: ■ sum(v) and mean(v) consume a vector and return a number ■ min(v) and max(v) return two quantities: the minimum or maximum value in a vector, plus the position in that vector where that value occurred. ■ round(v), ceil(v), floor(v) remove the fractional part of the numbers in a vector by conventional rounding, rounding up, rounding down, respectively. We can also apply functions like sqrt or sine or cosine to each element of a vector.