CSE123 Lecture 5 Arrays and Array Operations. Definitions Scalars: Variables that represent single numbers. Note that complex numbers are also scalars,

Slides:



Advertisements
Similar presentations
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.
Advertisements

3_3 An Useful Overview of Matrix Algebra
MF-852 Financial Econometrics
Arrays Revisited Selim Aksoy Bilkent University Department of Computer Engineering
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
Review of Matrix Algebra
GG313 Lecture 12 Matrix Operations Sept 29, 2005.
MOHAMMAD IMRAN DEPARTMENT OF APPLIED SCIENCES JAHANGIRABAD EDUCATIONAL GROUP OF INSTITUTES.
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.
Part 3 Chapter 8 Linear Algebraic Equations and Matrices PowerPoints organized by Dr. Michael R. Gustafson II, Duke University All images copyright © The.
Pam Perlich Urban Planning 5/6020
Matrix Mathematics in MATLAB and Excel
Lecture 7: Matrix-Vector Product; Matrix of a Linear Transformation; Matrix-Matrix Product Sections 2.1, 2.2.1,
Arithmetic Operations on Matrices. 1. Definition of Matrix 2. Column, Row and Square Matrix 3. Addition and Subtraction of Matrices 4. Multiplying Row.
CE 311 K - Introduction to Computer Methods Daene C. McKinney
1 Chapter 3 Matrix Algebra with MATLAB Basic matrix definitions and operations were covered in Chapter 2. We will now consider how these operations are.
1 Chapter 2 Matrices Matrices provide an orderly way of arranging values or functions to enhance the analysis of systems in a systematic manner. Their.
Chapter 7 Matrix Mathematics Matrix Operations Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Matlab intro The Environment
Chapter 10 Review: Matrix Algebra
MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson.
Fall 2006AE6382 Design Computing1 Vectors and Matrices Lecture Basic vector & matrix concepts Creating arrays and matrices Accessing matrix components.
Matlab tutorial course Lesson 2: Arrays and data types
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Introduction to MATLAB January 18, 2008 Steve Gu Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,
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,
Learner’s Guide to MATLAB® Chapter 2 : Working with Arrays.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
Statistics and Linear Algebra (the real thing). Vector A vector is a rectangular arrangement of number in several rows and one column. A vector is denoted.
CMPS 1371 Introduction to Computing for Engineers MATRICES.
Matrices Addition & Subtraction Scalar Multiplication & Multiplication Determinants Inverses Solving Systems – 2x2 & 3x3 Cramer’s Rule.
Lecture 28: Mathematical Insight and Engineering.
1 C ollege A lgebra Systems and Matrices (Chapter5) 1.
MATLAB Trigonometry, Complex Numbers and Array Operations.
Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element.
ES 240: Scientific and Engineering Computation. Chapter 8 Chapter 8: Linear Algebraic Equations and Matrices Uchechukwu Ofoegbu Temple University.
ME 142 Engineering Computation I Matrix Operations in Excel.
(The Transpose Operator) 1 >> C=[ ; ; ] C = >> D=C' D =
Introduction to Matlab Electromagnetic Theory LAB by Engr. Mian Shahzad Iqbal.
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
MT411 Robotic Engineering Asian Institution of Technology (AIT) Chapter 1 Introduction to Matrix Narong Aphiratsakun, D.Eng.
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)
Matrices and Matrix Operations. Matrices An m×n matrix A is a rectangular array of mn real numbers arranged in m horizontal rows and n vertical columns.
MATRIX A set of numbers arranged in rows and columns enclosed in round or square brackets is called a matrix. The order of a matrix gives the number of.
Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.
Linear System of Simultaneous Equations Warm UP First precinct: 6 arrests last week equally divided between felonies and misdemeanors. Second precinct:
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
Matrices. Matrix - a rectangular array of variables or constants in horizontal rows and vertical columns enclosed in brackets. Element - each value in.
Matrix Algebra Basics Chapter 3 Section 5. Algebra.
Matrix Algebra Definitions Operations Matrix algebra is a means of making calculations upon arrays of numbers (or data). Most data sets are matrix-type.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 Part 3 - Chapter 8 Linear Algebraic Equations and Matrices.
Matrices. Variety of engineering problems lead to the need to solve systems of linear equations matrixcolumn vectors.
10.4 Matrix Algebra. 1. Matrix Notation A matrix is an array of numbers. Definition Definition: The Dimension of a matrix is m x n “m by n” where m =
LAB 2 Vectors and Matrices Dr.Abdel Fattah FARES.
MTH108 Business Math I Lecture 20.
Linear Algebraic Equations and Matrices
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,
Chapter 7 Matrix Mathematics
Linear Algebraic Equations and Matrices
Introduction to MATLAB [Vectors and Matrices] Lab 2
Matrix Algebra.
Matrices in MATLAB Dr. Risanuri Hidayat.
Math review - scalars, vectors, and matrices
Matrix Operations Ms. Olifer.
Presentation transcript:

CSE123 Lecture 5 Arrays and Array Operations

Definitions Scalars: Variables that represent single numbers. Note that complex numbers are also scalars, even though they have two components. Arrays: Variables that represent more than one number. Each number is called an element of the array. Array operations allow operating on multiple numbers at once. Row and Column Arrays (Vector): A row of numbers (called a row vector) or a column of numbers(called a column vector). Two-Dimensional Arrays (Matrix): A two-dimensional table of numbers, called a matrix.

Vector Creation by Explicit List A vector in Matlab can be created by an explicit list, starting with a left bracket, entering the values separated by spaces (or commas) and closing the vector with a right bracket. >>x=[0.1*pi.2*pi.3*pi.4*pi.5*pi.6*pi.7*pi.8*pi.9*pi pi] >>y=sin(x) >>y = Columns 1 through Columns 8 through

Vector Addressing / indexation A vector element is addressed in Matlab with an integer index (also called a subscript) enclosed in parentheses. >> x(3) ans = >> y(5) ans = Colon notation: Addresses a block of elements. The format is: (start:increment:end) Note start, increment and end must be positive integer numbers. If the increment is to be 1, a shortened form of the notation may be used: (start:end) >> x(1:5) ans = >> x(7:end) ans = >> y(3:-1:1) ans = >> y([ ]) ans =

Vector Creation Alternatives Combining: A vector can also be defined using another vector that has already been defined. >> B = [1.5, 3.1]; >> S = [3.0 B] S = Changing: Values can be changed by referencing a specific address Extending: Additional values can be added using a reference to a specific address. >> S(2) = -1.0; >> S S = >> S(4) = 5.5; >> S S = >> S(7) = 8.5; >> S S =

Vector Creation Alternatives Colon notation: (start:increment:end) where start, increment, and end can now be floating point numbers. x=(0:0.1:1)*pi x = Columns 1 through Columns 8 through linspace: generates a vector of uniformly incremented values, but instead of specifying the increment, the number of values desired is specified. The form: linspace(start,end,number) The increment is computed internally, having the value:

Vector Creation Alternatives >> x=linspace(0,pi,11) x = Columns 1 through Columns 8 through logspace(start_exponent,end_exponent,number) To create a vector starting at 10 0 = 1,ending at 10 2 = 100 and having 11 values: >> logspace(0,2,11) ans = Columns 1 through Columns 8 through

Vector Length length(x): To determine the length of a vector array. >> x = [ ] x = >> length(x) ans = 6

Vector Orientation A column vector, having one column and multiple rows, can be created by specifying it element by element, separating element values with semicolons: >> c = [1;2;3;4;5] c = The transpose operator (’) is used to transpose a row vector into a column vector >> a = 1:5 a = >> c = a’ c =

Matrix arrays in Matlab Vector Use square brackets. Separate elements on the same row with spaces or commas. & Matrix Use semi-colon to go to the next row. A= [ ]; C= [ 5 ; 6 ; 7 ]; B= [ 1 2 ; 3 4 ]; A= [ 1, 2, 3 ];

Matrix Arrays A matrix array is 2D, having both multiple rows and multiple columns. Creation of 2D arrays follows that of row and column vectors: –Begin with [ end with ] –Spaces or commas are used to separate elements in a row. –A semicolon or Enter is used to separate rows. >> h = [ ] h = >> k = [1 2;3 4 5] ??? Number of elements in each row must be the same. >>f = [1 2 3; 4 5 6] f = >> g = f’ g =

Manipulations and Combinations: >> A=10*ones(2,2) A = Special matrix creation >> B=10*rand(2,2) Matrix full of 10: Matrix of random numbers between 0 and 10 B = >> C= -rand(2,2) Matrix of random numbers between -1 and 0 C = >> C=2*rand(2,2) –ones(2,2) >> C=2*rand(2,2) -1 Matrix of random numbers between -1 and 1 C =

Concatenation: Combine two (or more) matrices into one Special matrix creation Notation: C=[ A, B ] Square brackets >> A=ones(2,2); >> B=zeros(2,2); >>C=[A, B] >>D=[A ; B] D = C =

Obtain a single value from a matrix: Ex: want to know a 21 Matrix indexation Notation: A(2,1) Row index Column index >> A=[1 2 3; 3 2 1; 1 2 4]; >> A(2,1) ans = 3 >> A(3,2) ans = 2

Obtain more than one value from a matrix: Ex: X=1:10 Matrix indexation >> A=[1 2 3; 3 2 1; 1 2 4]; >> B=A(1:3,2:3) B = >> C=A(2,:) C = Colon Colon defines a “range”: 1 to 10 Notation: A(1:3,2:3) Row 1 to 3 Column 2 to 3 Colon can also be used as a “wildcard” Row 2, ALL columns

Matrix size CommandDescription s = size(A)For an m x n matrix A, returns the two-element row vector s = [m, n] containing the number of rows and columns in the matrix. [r,c] = size(A)[r,c] = size(A) Returns two scalars r and c containing the number of rows and columns in A, respectively. r = size(A,1)Returns the number of rows in A in the variable r. c = size(A,2)Returns the number of columns in A in the variable c.

Matrix size >> A = [1 2 3; 4 5 6] A = >> s = size(A) s = 2 3 >> [r,c] = size(A) r = 2 c = 3 >> whos Name Size Bytes Class A 2x3 48 double array ans 1x1 8 double array c 1x1 8 double array r 1x1 8 double array s 1x2 16 double array

zeros(M,N) Matrix of zeros ones(M,N) Matrix of ones eye(M,N) Matrix of ones on the diagonal rand(M,N) Matrix of random numbers between 0 and 1 >> A=zeros(2,3)A = >> B=ones(2,2)B = >> D=rand(3,2)D = Special matrix creation >> C=eye(2,2)C =

Operations on vectors and matrices in Matlab MathMatlab Addition/subtractionA+B A-B Multiplication/ division (element by element) A. * B A./B Multiplication (Matrix Algebra) A*BA*B Transpose: A T A’ Inverse: A -1 inv(A) Determinant: |A|det(A) “single quote”

Array Operations Scalar-Array Mathematics Addition, subtraction, multiplication, and division of an array by a scalar simply apply the operation to all elements of the array. >> f = [1 2 3; 4 5 6] f = >> g = 2*f -1 g =

Array Operations Element-by-Element Array-Array Mathematics When two arrays have the same dimensions, addition, subtraction, multiplication, and division apply on an element-by-element basis. Operation Algebraic Form Matlab Addition a + b a + b Subtraction a − b a - b Multiplication a x b a.*b Division a / b a./b Exponentiation a b a.^b

MATRIX Addition (substraction) M N M N M N Array Operations

Examples: Addition & Subtraction Array Operations

Element-by-Element Array-Array Mathematics >> A = [2 5 6]; >> B = [2 3 5]; >> C = A.*B C = >> D = A./B D = >> E = A.^B E = >> F = 3.0.^A F =

MATRIX Multiplication (element by element) M N M N M N “dot” “multiply” NOTATION Array Operations

Examples: Multiplication & Division (element by element) Array Operations

The matrix multiplication of m x n matrix A and nxp matrix B yields m x p matrix C, denoted by C = AB Element c ij is the inner product of row i of A and column j of B Note that AB ≠ BA Matrix Multiplication

Row 1 Column 1 Cell 1-1 “multiply” NOTATION M1M1 N1N1 M2M2 N2N2 M1M1 N2N2 N1=M2 Array Operations

Example: Matrix Multiplication x1 + 2x3 +3x3 1x2 + 2x2 +3x1 1x3 + 2x1 +3x2 Array Operations

Solving systems of linear equations Example: 3 equations and 3 unknown 1x + 6y + 7z =0 2x + 5y + 8z =1 3x + 4y + 5z =2 Can be easily solved by hand, but what can we do if it we have 10 or 100 equations? Array Operations

Solving systems of linear equations First, write a matrix with all the (xyz) coefficients 1x + 6y + 7z = 0 2x + 5y + 8z = 1 3x + 4y + 5z = 2 Write a matrix with all the constants Finally, consider the matrix of unknowns Array Operations

Solving systems of linear equations A x S = B A -1 x (A -1 x A) x S = A -1 x B I x S = A -1 x B S = A -1 x B Array Operations

Solving systems of linear equations 1x + 6y + 7z =0 2x + 5y + 8z =1 3x + 4y + 5z =2 The previous set of equations can be expressed in the following vector-matrix form: A x S = B X Array Operations

Matrix Determinant Notation: Determinant of A = |A| or det(A) The determinant of a square matrix is a very useful value for finding if a system of equations has a solution or not. If it is equal to zero, there is no solution. det(M)= m 11 m 22 – m 21 m 12 Formula for a 2x2 matrix: IMPORTANT: the determinant of a matrix is a scalar Array Operations

Matrix Inverse Notation: inverse of A = A -1 or inv(A) The inverse of a matrix is really important concept, for matrix algebra Calculating a matrix inverse is very tedious for matrices bigger than 2x2. We will do that numerically with Matlab. M -1 = Formula for a 2x2 matrix: IMPORTANT: the inverse of a matrix is a matrix Array Operations

Property of identity matrix: I x A = A and A x I = A Matrices properties Property of inverse : A x A -1 = I and A -1 x A = I Example: Array Operations

x + 6y + 7z =0 2x + 5y + 8z =1 3x + 4y + 5z =2 In Matlab: >> A=[ 1 6 7; 2 5 8; 3 4 5] >> B=[0;1;2]; >> S=inv(A)*B Verification: >> det(A) ans = 28 Solving systems of equations in Matlab >> S =

x + 6y + 7z =0 2x + 5y + 8z =1 3x + 4y + 9z =2 In Matlab: >> A=[ 1 6 7; 2 5 8; 3 4 5] >> B=[0;1;2]; >> S=inv(A)*B Verification: >> det(A) ans = 0 NO Solution!!!!! Solving systems of equations in Matlab Warning: Matrix is singular to working precision. >> S = NaN

Applications in mechanical engineering F1 F2 5N 7N x y 60 o 30 o 20 o 80 o Find the value of the forces F1and F2

F1 F2 5N 7N x y 60 o 30 o 20 o 80 o Projections on the X axisF1 cos(60) + F2 cos(80) – 7 cos(20) – 5 cos(30) = 0 Applications in mechanical engineering

F1 F2 5N 7N x y 60 o 30 o 20 o 80 o Projections on the Y axisF1 sin(60) - F2 sin(80) + 7 sin(20) – 5 sin(30) = 0 Applications in mechanical engineering

F1 cos(60) + F2 cos(80) – 7 cos(20) – 5 cos(30) = 0 F1 sin(60) - F2 sin(80) + 7 sin(20) – 5 sin(30) = 0 F1 cos(60) + F2 cos(80) = 7 cos(20) + 5 cos(30) F1 sin(60) - F2 sin(80) = - 7 sin(20) + 5 sin(30) In Matlab: >> CF=pi/180; >> A=[cos(60*CF), cos(80*CF) ; sin(60*CF), –sin(80*CF)]; >> B=[7*cos(20*CF)+5*cos(30*CF) ; -7*sin(20*CF)+5*sin(30*CF) ] >> F= inv(A)*B or (A\B) F = In Matlab, sin and cos use radians, not degree Solution: F1= N F2= N Applications in mechanical engineering