Arrays in Matlab UC Berkeley Fall 2004, E Copyright 2005, Andy Packard

Slides:



Advertisements
Similar presentations
Introduction to Matlab
Advertisements

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.
BIL101, Introduction to Computers and Information Systems Chapter 12 A Portable Scientific Visualization Program: GnuPlot Prepared by Metin Demiralp Istanbul.
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
EGR 106 – Week 3 – More on Arrays Brief review of last week Additional ideas: – Special arrays – Changing an array – Some array operators – Character arrays.
Matrix Definition A Matrix is an ordered set of numbers, variables or parameters. An example of a matrix can be represented by: The matrix is an ordered.
Functions UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
MATLAB Fundamentals.
1 Introduction to MATLAB MATLAB is all of the following: 1.Computational environment 2.Plotting software 3.Programming language Typical applications: 1.Calculations.
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
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
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.
Time Complexity UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Learner’s Guide to MATLAB® Chapter 2 : Working with Arrays.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
Cell Arrays UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
13.1 Matrices and Their Sums
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.
Script Files UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
MATLAB Lecture Two Tuesday 5 July Chapter 3.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
Struct Arrays UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Prepared by Deluar Jahan Moloy Lecturer Northern University Bangladesh
Polynomials UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
(The Transpose Operator) 1 >> C=[ ; ; ] C = >> D=C' D =
Basics of Matlab UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Matrices: Simplifying Algebraic Expressions Combining Like Terms & Distributive Property.
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
Introduction to Engineering MATLAB – 4 Arrays Agenda Creating arrays of numbers  Vectors: 1-D Arrays  Arrays: 2-D Arrays Array Addressing Strings & String.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
MATLAB Constants, Variables & Expression Nafees Ahmed Asstt. Professor, EE Deptt DIT, DehraDun.
Intro to Matlab Rogelio Long September 3, How to access MyDesktop Log in with your utep id and password.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Matlab Tutorial Iman Moazzen First Session – September 11, 2013.
Project Teams Project on Diffusion will be a TEAM project. 34 Students in class so 6 teams of 5 and one Team of 4. Determine members of team and a team.
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
ECE 1304 Introduction to Electrical and Computer Engineering
Introduction to MATLAB for Engineers, Third Edition
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,
For/Switch/While/Try UC Berkeley Fall 2004, E77 me
Seminar 1 for DCSP Using Matlab.
MATLAB DENC 2533 ECADD LAB 9.
Matlab Workshop 9/22/2018.
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
Part 1 Chapter 2 MATLAB Fundamentals
MATH 493 Introduction to MATLAB
Matlab tutorial course
MATRICES MATRIX OPERATIONS.
2.2 Introduction to Matrices
Introduction to MATLAB
Arrays and Matrices in MATLAB
Introduction to Matlab
INTRODUCTION TO MATLAB
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
Part 1 Chapter 2 MATLAB Fundamentals
E177, Reference/Assignment for Classes: Intro February 12, Copyright , Andy Packard. This work.
Function Handles UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Announcements P3 due today
3.5 Perform Basic Matrix Operations
CS 111 Introduction to Computing in Engineering and Science
-seminar 1 for digital signal processing
Working with Arrays in MATLAB
Basics of Matlab UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Presentation transcript:

Arrays in Matlab UC Berkeley Fall 2004, E77 http://jagger.me.berkeley.edu/~pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons Attribution-ShareAlike License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

A rectangular arrangement of numbers is called an array. Arrays A rectangular arrangement of numbers is called an array. Later, we will have rectangular arrangement of more general objects. Those will also be called arrays. This is a 4-by-2 array. It has 4 rows, and 2 columns. The (3,1) entry is the entry in the 3rd row, and 1st column

Scalars, Vectors and Arrays 1-by-1 arrays are also called scalars 1-by-N arrays are also called row vectors N-by-1 arrays are also called column vectors Row and Column vectors are also sometimes just called vectors In Matlab, all arrays of numbers are called double arrays.

Creating Arrays Horizontal and Vertical Concatenation (ie., “stacking”) Square brackets, [, and ] to define arrays Spaces (and/or commas) to separate columns Semi-colons to separate rows Example >> [ 3 4 5 ; 6 7 8 ] is the 2-by-3 array If A and B are arrays with the same number of rows, then >> C = [ A B ] is the array formed by stacking A “next to” B Once constructed, C does not “know” that it came from two arrays stacked next to one another. No partitioning information is maintained. If A and B are arrays with the same number of columns, then >> [ A ; B ] is the array formed by stacking A “on top of” B So, [ [ 3 ; 6 ] [ 4 5 ; 7 8 ] ] is equal to [ 3 4 5;6 7 8 ]

Creating special arrays ones(n,m) a n-by-m double array, each entry is equal to 1 zeros(n,m) a n-by-m double array, each entry is equal to 0 rand(n,m) a n-by-m double array, each entry is a random number between 0 and 1. Examples >> A = ones(2,3); >> B = zeros(3,4); >> C = rand(2,5);

: convention The “: (colon) convention” is used to create row vectors, whose entries are evenly spaced. 7:2:18 equals the row vector [ 7 9 11 13 15 17 ] If F, J and L are numbers with J>0, F ≤ L, then F:J:L creates a row vector [ F F+J F+2*J F+3*J … F+N*J ] where F+N*J ≤ L, and F+(N+1)*J>L Many times, the increment is 1. Shorthand for F:1:L is F:L

F:J:L with J<0 (decrementing) You can also decrement (ie., J<0), in which case L should be less than F. Therefore 6.5:-1.5:0 is the row vector [ 6.5 5.0 3.5 2.0 0.5 ]

The SIZE command If A is an array, then size(A) is a 1-by-2 array. The (1,1) entry is the number of rows of A The (1,2) entry is the number of columns of A If A is an array, then size(A,1) is the number of rows of A size(A,2) is the number of columns of A Example >> A = rand(5,6); >> B = size(A) >> size(A,2)

Accessing single elements of a vector If A is a vector (ie, a row or column vector), then A(1) is its first element, A(2) is its second element,… Example >> A = [ 3 4.2 -7 10.1 0.4 -3.5 ]; >> A(3) >> Index = 5; >> A(Index) This syntax can be used to assign an entry of A. Recall assignment >> VariableName = Expression An entry of an array may also be assigned >> VariableName(Index) = Expression So, change the 4’th entry of A to the natural logarithm of 3. >> A(4) = log(3);

Accessing multiple elements of a vector A(3) refers to the 3rd entry of A. However, the index need not be a single number. Example: Make a 1-by-6 row vector, and access multiple elements, giving back row vectors of various dimensions. >> A = [ 3 4.2 -7 10.1 0.4 -3.5 ]; >> A([1 4 6]) % 1-by-3, 1st, 4th, 6th entry >> Index = [3 2 3 5]; >> A(Index) % 1-by-4 Index should contain integers. Regardless of whether A is a row or column, Index can be a row or a column, and Matlab will do the same thing in both cases. The expressions below are the same. >> A([2 4 3]) >> A([2;4;3])

Assigning multiple elements of a vector In an assignment to multiple entries of a vector >> A(Index) = Expression the right-hand side expression should be a scalar, or the same size as the array being referenced by A(Index) Example: Make a 1-by-6 row vector, and access multiple elements, giving back row vectors of various dimensions. >> A = [ 3 4.2 -7 10.1 0.4 -3.5 ]; >> A([1 4 6]) = [10 100 1000]; >> Index = [3 2 3 5]; >> A(Index) = pi;

Accessing elements and parts of arrays If M is an array, then M(3,4) is the element in the (3rd row , 4th column) of M If M is an array, then M([1 4 2],[5 6]) is a 3-by-2 array, consisting of the entries of M from rows [1, 4 and 2] columns [5 and 6] In an assignment >> M(RIndex,CIndex) = Expression the right-hand side expression should be a scalar, or the same size as the array being referenced by M(RIndex,CIndex) Do some examples

Layout in memory The entries of a numeric array in Matlab are stored together in memory in a specific order. >> A = [ 3 4.2 8 ; -6.7 12 0.75 ]; represents the array Somewhere in memory, Matlab has Name = ‘A’ Size = [2 3]; Data = Workspace: Base 3 -6.7 4.2 12 8 0.75

RESHAPE So, A is the array Name = ‘A’ Size = [2 3]; Data = Name = ‘B’ RESHAPE changes the size, but not the values or order of the data in memory. >> A = [ 3 4.2 8 ; -6.7 12 0.75 ]; >> B = reshape(A,[3 2]); The result (in memory is) So, A is the array Name = ‘A’ Size = [2 3]; Data = Name = ‘B’ Size = [3 2]; Data = 3 -6.7 4.2 12 8 0.75 3 -6.7 4.2 12 8 0.75 while B is the array

>> M([1 end],[end-2:end]) Suppose A is an N-by-M array, and a reference of the form A(RIndex,CIndex) Any occurence of the word end in the RIndex is changed (automatically) to N Any occurence of the word end in the CIndex is changed (automatically) to M Example: >> M = rand(4,5); >> M(end,end) >> M([1 end],[end-2:end])

: as a row or column index Suppose A is an N-by-M array, and a reference of the form A(RIndex,CIndex) If RIndex is a single colon, :, then RIndex is changed (automatically) to 1:N (every row) If CIndex is a single colon, :, then CIndex is changed (automatically) to 1:M (every column) Example: >> M = rand(4,5); >> M(:,[1 3 5])

>> log10(logspace(-1,1,5)) LINSPACE and LOGSPACE >> linspace(A,B,N) is a 1-by-N row vector of evenly spaced numbers, starting at A, and ending at B >> logspace(A,B,N) is a 1-by-N row vector of logarithmically spaced numbers, starting at 10A, and ending at 10B. Examples >> linspace(1,4,6) >> logspace(-1,1,5) >> log10(logspace(-1,1,5))

Unary Numeric Operations on double Arrays Unary operations involve one input argument. Examples are: Negation, using the “minus” sign Trig functions, sin, cos, tan, asin, acos, atan,… General rounding functions, floor, ceil, fix, round Exponential and logs, exp, log, log10, sqrt Complex, abs, angle, real, imag Example: If A is an N1-by-N2-by-N3-by-… array, then B = sin(A); is an N1-by-N2-by-N3-by-… array. Every entry of B is the sin of the corresponding entry of A. The “for”-loop that cycles the calculation over all array entries is an example of the vectorized nature of many Matlab builtin functions

Binary (two arguments) operations on Arrays Addition (and subtraction) If A and B are arrays of the same size, then A+B is an array of the same size whose individual entries are the sum of the corresponding entries of A and B If A is an array and B is a scalar, then A+B is an array of the same size as A, whose individual entries are the sum of the corresponding entries of A and the scalar B If A is a scalar, and B is an array, use same logic as above Scalar-Array Multiplication If A is an array,and B is a scalar, then A*B is an array of the same size as A, whose individual entries are the product of the corresponding entries of A and the scalar B. Element-by-Element Multiplication If A and B are arrays of the same size, then A.*B is an array of the same size whose individual entries are the product of the corresponding entries of A and B Matrix multiplication If A and B are arrays, then A*B is the matrix multiplication of the two arrays… More later

Intro to plotting with Matlab If X is a 1-by-N (or N-by-1) vector, and Y is a 1-by-N (or N-by-1) vector, then >> plot(X,Y) creates a figure window, and plots the data in the axis. The points plotted are (X(1),Y(1)), (X(2),Y(2)), … , (X(N),Y(N)). By default, Matlab will draw straight lines between the data points, and the points will not be explicitly marked. For more info, do >> help plot Example: >> X = linspace(0,3*pi,1000); >> Y = sin(X);

Plotting several lines If X1 and Y1 are both 1-by-N, and X2 and Y2 are both 1-by-M, then >> plot(X1,Y1,X2,Y2) will plot both sets of data on the same axis. Example >> X1 = linspace(0,pi,1000); >> Y1 = cos(4*X1).*sin(X1); >> X2 = [0 1 4 5]; >> plot(X1,Y1,X2,sqrt(X2))