Arrays  An array is a collection of like elements.  There are many engineering applications that use arrays.  MATLAB ® stores data in arrays and performs.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Introduction to MATLAB The language of Technical Computing.
Introduction to Matlab
Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction.
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
Maths for Computer Graphics
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.
Multidimensional Arrays in Java Vidhu S. Kapadia.
Arrays. 2 The array data structure An array is an indexed sequence of components Typically, the array occupies sequential storage locations The length.
C ENTER FOR I NTEGRATED R ESEARCH C OMPUTING MATLAB
Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections.
Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Java Unit 9: Arrays Declaring and Processing Arrays.
CSC – Java Programming II Lecture 9 January 30, 2002.
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
Martin Ellison University of Warwick and CEPR Bank of England, December 2005 Introduction to MATLAB.
Arrays in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR
Matlab Chapter 2: Array and Matrix Operations. What is a vector? In Matlab, it is a single row (horizontal) or column (vertical) of numbers or characters.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Array Addition  Two arrays can be added if and only if both arrays have exactly the same dimensions.  Assuming the dimension requirement is satisfied,
Comp 248 Introduction to Programming Chapter 6 Arrays Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Working with Arrays in MATLAB
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
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.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Digital Image Processing Introduction to M-function Programming.
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
Introducing Arrays We will often need to store collections of information –a list of names to sort –a list of values to compute averages, standard deviation,
1 Arrays of Arrays An array can represent a collection of any type of object - including other arrays! The world is filled with examples Monthly magazine:
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 1: Variables & Arrays Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers.
1 Lecture 3 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Computer Programming for Engineers
1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:
Chapter 8: Part 3 Collections and Two-dimensional arrays.
Improving Matlab Performance CS1114
Chapter 8: Arrays Gator Engineering One-dimensional array Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Move the first element to the.
Digital Image Processing. Converting between Data Classes The general syntax is B = data_class_name (A) Where data_class_name is one of the names defined.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Multidimensional Arrays Computer and Programming.
Cell Arrays: An Introduction Without an understanding of how cell arrays work and how to interact with them, cell arrays can be one of the most frustrating.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
ARRAYS Multidimensional realities Image courtesy of
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.
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,
© 2016 Pearson Education, Ltd. All rights reserved.
L – Modeling and Simulating Social Systems with MATLAB
EGR 115 Introduction to Computing for Engineers
Other Kinds of Arrays Chapter 11
Scripts & Functions Scripts and functions are contained in .m-files
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
Matlab tutorial course
Introduction to MATLAB [Vectors and Matrices] Lab 2
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
Working with Arrays in MATLAB
Presentation transcript:

Arrays  An array is a collection of like elements.  There are many engineering applications that use arrays.  MATLAB ® stores data in arrays and performs all numerical computations using array operations. Therefore, to use MATLAB ® effectively as a computing tool, one must understand arrays and operations with arrays.

1-d Arrays: Vectors A vector is a one-dimensional array. Examples: A row vector with 4 elements x = [ ] A column vector with 3 elements y = x(3) = 4 (3 rd element in vector x) y(2) = 2.9 (2 nd element in vector y)

2-d Arrays: Matrices  A matrix is a two-dimensional array.  The size of a matrix is the number of rows by the number of columns. Examples: A = B = x3 Matrix 3x4 Matrix A(2, 3) = 10 (element in row 2 and column 3) B(1,4) = 82 (element in row 1 and column 4)

Multi-Dimensional Arrays Arrays can have more than two dimensions. For example, a 3648 x 2736 jpg color image imported into MATLAB ® would be a 3-dimensional array of size 3648 x 2736 x 3 where the 3 rd dimension represents the RGB panes as illustrated below rows of pixel data 2736 columns of pixel data 3 color panes

Creating Arrays in MATLAB® >> a = [ ] a = >> b = [2.3; 7.5; 4.3; 6] b = >> c = [ ; ] c = >> d = [ ; ] Error using vertcat Dimensions of matrices being concatenated are not consistent. Note: Commas can be used between values in a row instead of spaces

Creating Arrays in MATLAB® FUNCTIONDESCRIPTION A:INT:B Creates a row vector of values starting at A, spaced by INT, terminating at B or just below B. linspace(A, B, N) Creates a row vector of N values equally spaced from A to B eye(n,n)Creates an n x n identity matrix zeros(n,m)Creates an n x m matrix of zeros ones(n,m)Creates an n x m matrix of ones Caution: zeros(n) produces an n x n square matrix of zeros, not a vector of zeros ones(n) produces an n x n square matrix of ones, not a vector of ones

Creating Arrays in MATLAB® FUNCTIONDESCRIPTION randi([Imin,Imax], [n,m]) Creates an n x m matrix of integers uniformly distributed from Imin to Imax randn(n,m) Creates an n x m matrix of random numbers normally distributed with mean 0 and standard deviation of 1. rand(n,m) Creates an n x m matrix of random numbers uniformly distributed on the interval [0 1] randperm(n,m) Creates vector of m unique integers selected from the values 1, 2, … n Note: The MATLAB command: rng('shuffle') seeds the random number generator based on the current time so that RAND, RANDI, and RANDN will produce a different sequence of numbers after each time you call rng.

Indexing Vectors in MATLAB®  In MATLAB ®, array indexing starts with 1. This is different from many other programming languages including java, C, and C++ where array indexing starts with 0.  x(1) is the 1 st element in the vector x, x(2) is the 2 nd element in the vector x, … Suppose we create a vector: >> x = [   3 2 4] x is a vector with 8 values. To pull out a specific value or a set of values in x, we must index into the array, x.

Indexing Vectors in MATLAB®  x(3:7) would be a vector with the 3 rd through 7 th entries of vector x. An error would be produced if x had less than 7 entries.  To find out how many entries are in a vector x, use the command: >> N = length(x)

Example >> x = [ ] >> x( [1 6 8]) %entries 1, 6 & 8 ans = >> x(3 ) >> x(2:6 ) >> x(1:2:8 ) >> x( [1 6 8] ) >> x(3) % 3 rd entry ans = 1 >> x(2:6) % entries 2 thru 6 ans = >> x(1:2:8) %entries 1, 3, 5 & 7 ans = START MAX.

Indexing Matrices in MATLAB® Assume A is a matrix defined in MATLAB that has at least 4 rows and at least 7 columns.  A(3,5) is the entry in row 3, column 5 of matrix A.  A(3,:) is a vector consisting of the entire 3 rd row of matrix A. The : captures all columns.  A(:,2) is a vector consisting of the entire 2 nd column of matrix A. The : captures all the rows.  A(2:4, 3:5) is a 3 x 3 matrix formed from rows 2,3, and 4 and columns 3,4, and 5 of matrix A.  To determine how many rows and columns a matrix has, use the command: >> [rows,cols] = size(A)

Matrix Example >> matrix = randi([0 10],[4,5]) matrix = >> matrix(2,3) >> matrix(:,4) >> matrix(3,2:4) >> matrix(2:3,3:5) >> matrix(2,3) ans = 10 >> matrix(:,4) ans = >> matrix(3,2:4) ans = >> matrix(2:3,3:5) ans =

Some Useful MATLAB® Functions for Indexing FUNCTIONDESCRIPTION size(A)Gives the dimensions of array A. For matrix (2-d array), gives the number of rows and columns. length(A)Gives the largest dimension of array A. Most useful for vectors (one-dimensional arrays) since it will tell you how many elements are in the vector.

Looping thru a 1-d Array What will the following code produce? x = [ ] for k = 1:length(x) if x(k) < 0 x(k) = 0; end disp(‘x is now:’); disp(x)

Looping thru a 1-d Array x = [ ] for k = 1:length(x) if x(k) < 0 x(k) = 0; end disp(‘x is now:’); disp(x) k = 1 x(1) < 0? Yes Set x(1) = 0 x = [ ] k = 2 x(2) < 0? No x = [ ] k = 3 x(3) < 0? No x = [ ] k = 4 x(4) < 0? Yes Set x(4) = 0 x = [ ] k = 5 x(5) < 0? No x is now:

Looping thru 2-d Array Need a nested loop for a matrix. What will this code produce? A = [-1 3 5; ; ] [TotalRows TotalCols] = size(A); for r = 1:TotalRows for c = 1:TotalCols if A(r,c) < 0 B(r,c) = 0; elseif A(r,c) > 5 B(r,c) = 5; else B(r,c) = A(r,c); end disp(‘Matrix B =’);disp(B)

Looping thru 2-d Array A = [-1 3 5; ; ] [TotalRows TotalCols] = size(A); for r = 1:TotalRows for c = 1:TotalCols if A(r,c) < 0 B(r,c) = 0; elseif A(r,c) > 5 B(r,c) = 5; else B(r,c) = A(r,c); end disp(‘Matrix B =’);disp(B) r = 1 and c = 1 A(1,1) < 0? Yes B(1,1) = 0 B = 0

Looping thru 2-d Array A = [-1 3 5; ; ] [TotalRows TotalCols] = size(A); for r = 1:TotalRows for c = 1:TotalCols if A(r,c) < 0 B(r,c) = 0; elseif A(r,c) > 5 B(r,c) = 5; else B(r,c) = A(r,c); end disp(‘Matrix B =’);disp(B) r = 1 and c = 2 A(1,2) < 0? No A(1,2) > 5? No B(1,2) = A(1,2) B = 0 3

Looping thru 2-d Array A = [-1 3 5; ; ] [TotalRows TotalCols] = size(A); for r = 1:TotalRows for c = 1:TotalCols if A(r,c) < 0 B(r,c) = 0; elseif A(r,c) > 5 B(r,c) = 5; else B(r,c) = A(r,c); end disp(‘Matrix B =’);disp(B) r = 1 and c = 3 A(1,3) < 0? No A(1,3) > 5? No B(1,3) = A(1,3) B = 0 3 5

Looping thru 2-d Array A = [-1 3 5; ; ] [TotalRows TotalCols] = size(A); for r = 1:TotalRows for c = 1:TotalCols if A(r,c) < 0 B(r,c) = 0; elseif A(r,c) > 5 B(r,c) = 5; else B(r,c) = A(r,c); end disp(‘Matrix B =’);disp(B) r = 2 and c = 1 A(2,1) < 0? No A(2,1) > 5? Yes B(2,1) = 5 B =

Looping thru 2-d Array A = [-1 3 5; ; ] [TotalRows TotalCols] = size(A); for r = 1:TotalRows for c = 1:TotalCols if A(r,c) < 0 B(r,c) = 0; elseif A(r,c) > 5 B(r,c) = 5; else B(r,c) = A(r,c); end disp(‘Matrix B =’);disp(B) r = 2 and c = 2 A(2,2) < 0? Yes B(2,2) = 0 B =

Looping thru 2-d Array A = [-1 3 5; ; ] [TotalRows TotalCols] = size(A); for r = 1:TotalRows for c = 1:TotalCols if A(r,c) < 0 B(r,c) = 0; elseif A(r,c) > 5 B(r,c) = 5; else B(r,c) = A(r,c); end disp(‘Matrix B =’);disp(B) r = 2 and c = 3 A(2,3) < 0? No A(2,3) > 5? No B(2,3) = A(2,3) B =

Looping thru 2-d Array A = [-1 3 5; ; ] [TotalRows TotalCols] = size(A); for r = 1:TotalRows for c = 1:TotalCols if A(r,c) < 0 B(r,c) = 0; elseif A(r,c) > 5 B(r,c) = 5; else B(r,c) = A(r,c); end disp(‘Matrix B =’);disp(B) r = 3 and c = 1 A(3,1) < 0? No A(3,1) > 5? No B(3,1) = A(3,1) B =

Looping thru 2-d Array A = [-1 3 5; ; ] [TotalRows TotalCols] = size(A); for r = 1:TotalRows for c = 1:TotalCols if A(r,c) < 0 B(r,c) = 0; elseif A(r,c) > 5 B(r,c) = 5; else B(r,c) = A(r,c); end disp(‘Matrix B =’);disp(B) r = 3 and c = 2 A(3,2) < 0? Yes B(3,2) = 0 B =

Looping thru 2-d Array A = [-1 3 5; ; ] [TotalRows TotalCols] = size(A); for r = 1:TotalRows for c = 1:TotalCols if A(r,c) < 0 B(r,c) = 0; elseif A(r,c) > 5 B(r,c) = 5; else B(r,c) = A(r,c); end disp(‘Matrix B =’);disp(B) r = 3 and c = 3 A(3,3) < 0? No A(3,3) > 5? Yes B(3,3) = 5 Matrix B =

Pre-Allocating Memory Space In the previous program, the matrix B was created within the loops one entry at a time and therefore got larger with every cycle through the loop. If an array grows in size during the execution of a program, MATLAB must keep re-allocating space for the array which can be time-consuming. If you know ahead of time how big your array will be, you can avoid this problem by pre-allocating space for the array and filling the space with some numbers (often zeros).

Pre –Allocating Space A = [-1 3 5; ; ] B = zeros(size(A)); % Create a matrix B of 0s [TotalRows TotalCols] = size(A); for r = 1:TotalRows for c = 1:TotalCols if A(r,c) < 0 B(r,c) = 0; elseif A(r,c) > 5 B(r,c) = 5; else B(r,c) = A(r,c); end disp(‘Matrix B =’);disp(B)

Logic and Relational Operations FUNCTIONDESCRIPTION A == B Entry by entry check for aij == bij. Produces a 1 if aij == bij and 0 otherwise. A > B Entry by entry check for aij > bij. Produces a 1 if aij > bij and 0 otherwise. A < B Entry by entry check for aij < bij. Produces a 1 if aij < bij and 0 otherwise. A & BEntry by entry logical and operation: aij & bij Produces a 0 if either entry is 0 (false). Otherwise, produces a 1. A | B Entry by entry logical or operation: aij | bij Produces 0 if both entries are 0 (false). Otherwise, produces a 1. A and B must have the same dimensions unless one is a scalar Remember, 0 = FALSE and Non-Zero = TRUE

Examples >> A = randi ( [-10 10], [4 3] ) A = >> A > 5 ans =

Examples >> A = [-1 5; 3 4] A = >> B = [2 10; 3 1] B = >> A == B ans =

Examples >> A = [-1 5; 3 4] A = >> B = [2 10; 3 1] B = >> A > B ans =

Examples >> A = [-1 5; 3 4] A = >> B = [2 10; 3 1] B = >> A >= B ans =

Indexing into a String Each letter in a string is stored as a separate entry in a regular array: >> name='JohnSmith' name = JohnSmith >> name(1) ans = J >> name(2) ans =o >> name(10) ??? Index exceeds matrix dimensions.

Cell Arrays Regular Arrays do not work well at all for strings! Use Cell Arrays instead. Cell arrays work extremely well for handling strings and for handling mixed data types. For cell arrays, use curly braces { } rather than square brackets [ ] to enter the array. Other than that, indexing works exactly the same.

Cell Array Example >> Months = { 'January' 'February' 'March' 'April' 'May'} Months = 'January' 'February' 'March' 'April' 'May' >> Months(1) ans = 'January' >> Months(5) ans = 'May'

Cell Array Example (con’t) >> strcmp(Months,'February') % String Comparison ans = >> strcmp(Months,'february') % Case Sensitive ans = >> strcmpi(Months,'february') % Case In-sensitive ans = >> strncmp(Months,'Feb',3) % Compare 1 st 3 letters ans =

Comments  Regular arrays are very convenient for doing many different types of numerical computations and for many programming applications.  All entries in a regular array must be of the same type. You cannot have some entries that are doubles and some entries that are integers (type uint8 for examples).  A cell array allows for mixed data types of varying lengths. It is useful for strings and for mixing strings and numbers.

Your Turn Try the following commands in MATLAB >> x = [ ] >> x(3) >> x(10) >> x(3:5) >> x([1 5]) >> N = length(x) >> x(2:4) = [1 2 3];

Your Turn Try the following commands in MATLAB >> A = [ ; ; ] >> A(2,3) >> A(1,5) >> A(1,:) >> A(:,3) >> A(2:3,1:2) >> [Rows Cols] = size(A) >> A(2,3:4) = [0 0]

Your Turn Try the following commands in MATLAB >> A = [-1 5 7; ] >> B = [ 2 3 4; ] >> A > 0 >> A == 0 >> A == B >> A > B

Test Your Understanding