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.

Slides:



Advertisements
Similar presentations
Chapter Matrices Matrix Arithmetic
Advertisements

Matrices & Systems of Linear Equations
Maths for Computer Graphics
Matrices. Special Matrices Matrix Addition and Subtraction Example.
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.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
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.
Matlab Intro. Outline Matlab introduction Matlab elements Types Variables Matrices.
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.
Matrices and Determinants
Lecture 7: Matrix-Vector Product; Matrix of a Linear Transformation; Matrix-Matrix Product Sections 2.1, 2.2.1,
Intro to Matrices Don’t be scared….
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.
Chapter 7 Matrix Mathematics Matrix Operations Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Matrix Solution of Linear Systems The Gauss-Jordan Method Special Systems.
Chapter 10 Review: Matrix Algebra
Compiled By Raj G. Tiwari
ENGR-25_Arrays-2.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.
Sundermeyer MAR 550 Spring Laboratory in Oceanography: Data and Methods MAR550, Spring 2013 Miles A. Sundermeyer Linear Algebra & Calculus Review.
ECON 1150 Matrix Operations Special Matrices
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.
Matrix Algebra. Quick Review Quick Review Solutions.
Euclidean m-Space & Linear Equations Row Reduction of Linear Systems.
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,
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.
Chapter 2 Numeric, Cell, and Structure Arrays. Physics Connection - Specification of a position vector using Cartesian coordinates. Figure 2.1–1 2-2 The.
CMPS 1371 Introduction to Computing for Engineers MATRICES.
8.1 Matrices & Systems of Equations
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.
Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Prepared by Deluar Jahan Moloy Lecturer Northern University Bangladesh
>> x = [ ]; y = 2*x y = Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions.
Chapter 2 Creating Arrays Legend: MATLAB command or syntax : Blue MATLAB command OUTPUT : LIGHT BLUE.
ES 240: Scientific and Engineering Computation. Chapter 8 Chapter 8: Linear Algebraic Equations and Matrices Uchechukwu Ofoegbu Temple University.
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
3.4 Solution by Matrices. What is a Matrix? matrix A matrix is a rectangular array of numbers.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 1: Variables & Arrays Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers.
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.
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:
A L I MAM M OHAMMAD B IN S AUD I SLAMIC U NIVERSITY C OLLEGE OF S CIENCES D EPARTMENT OF M ATHEMATICS MATLAB 251 : MATH SOFTWARE Introduction to MATLAB.
Matrices. Variety of engineering problems lead to the need to solve systems of linear equations matrixcolumn vectors.
CS 285- Discrete Mathematics Lecture 11. Section 3.8 Matrices Introduction Matrix Arithmetic Transposes and Power of Matrices Zero – One Matrices Boolean.
LAB 2 Vectors and Matrices Dr.Abdel Fattah FARES.
MTH108 Business Math I Lecture 20.
Linear Algebraic Equations and Matrices
Introduction to MATLAB for Engineers, Third Edition
MATRICES.
1.5 Matricies.
Introduction to MATLAB 7
Linear Algebraic Equations and Matrices
Matrix Operations.
Warm-up a. Solve for k: 13 −5
MATRICES MATRIX OPERATIONS.
Array and Matrix Functions
Introduction to MATLAB 7
EECS Introduction to Computing for the Physical Sciences
MATRICES MATRIX OPERATIONS.
Presentation transcript:

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. In Matlab, it is a single row (horizontal) or column (vertical) of numbers or characters. Vector is one row OR one column Vector is one row OR one column –Does not have to have anything to do with geometry –Number of components is not restricted –Transpose of row is column, of column is row

Vectors in Matlab Various forms for entering row and column vectors: Various forms for entering row and column vectors: >> p=[1,2,3] p = >> u=[1;2;3;4] u = >> v=[3:2:9] v = >> w=[4:7] w =

Transposing Vectors >> p p = >> p' ans = >> u u = >> u' ans =

Augmenting Row Vectors >> p p = >> v v = >> z=[p v] z =

Stacking Column Vectors >> u u = >> t t = >> q=[u;t] q =

Functions to Generate Vectors linspace(a,b,c) produces c evenly spaced points between a and b linspace(a,b,c) produces c evenly spaced points between a and b >> linspace(2,4,5) ans = logspace(a,b,n) computes n pts linearly between a and b, then uses them as exponents of 10 >> logspace(1,3,4) ans = 1.0e+003 *

Matrices A rectangular array of numbers or characters A rectangular array of numbers or characters –Rows numbered top to bottom –Columns numbered left to right –Size of array: n x m »number of rows is always stated first –element of array: x(k,j) »row index is always stated first—kth row, jth col –In general, indices can be negative or zero, but not in MATLAB –Transpose of real matrix interchanges rows and columns

Transpose & Concatenate Matrices >> A=[1,2,3;4,5,6] >> A=[1,2,3;4,5,6] A = A’ is transpose >> A' ans = >> B=[4,5;8,9] B = [A,B] augments (concatenates rows) ans = [A’;B] stacks (concatenates cols) ans =

Vector Addressing »v = [1,7,3,8,6,7,3] v(4) returns the 4 th element >> v(4)v(4) returns the 4 th element >> v(4) ans = 8 Note: parentheses, not square brackets! v(2:5) returns elements 2 through 5v(2:5) returns elements 2 through 5 >> v(2:5) ans =

Vector Addressing cont. »v = [1,7,3,8,6,7,3] v(:) returns the entire vector as a columnv(:) returns the entire vector as a column >> v(:) ans =

Matrix Addressing A = A(2,1) returns the element in the 2 nd row, 1 st column »ans = 1 A(:,3) returns the 3 rd column »ans = 8 2 A(2,:) returns the 2 nd row »ans = A(1:2,2:3) returns all elements in the 1 st and 2 nd rows that are also in 2 nd and 3 rd columns

Matrix Addressing A = A(1:2,2:3) returns all elements in the 1 st and 2 nd rows that are also in 2 nd and 3 rd columns ans = 6 8 A(1:2,2:3) returns all elements in the 1 st and 2 nd rows that are also in 2 nd and 3 rd columns ans = Extract smaller array: Extract smaller array: »B = A(1:2,[1 3]) B =

Arrays/Matrix Addressing Empty or null array Empty or null array »[ ] A = –Remove a column: »A(:,3) = [ ] A = –Automatic enlargement: »A(3,5) = 22 A=

Arrays/Matrix Addressing –Negative increment reverses order: p=[3,8,4,6]; p=[3,8,4,6]; »rev = p(end:-1:1) rev = –A(n) returns the n th element of matrix A, going column by column A = »A(4) ans = 5

Arrays/Matrix Addressing A(:) returns all elements as a column, going column by column A = »A(:) ans =

Automatic growth of a vector If vector already defined as row or column, assigning new element beyond current bounds  automatically grows to fit with intervening elements set to 0 If vector already defined as row or column, assigning new element beyond current bounds  automatically grows to fit with intervening elements set to 0 »x = [1;2]% x starts as 2-element column vector »x(5) = 24% x now has 5 elements. x(3) & x(4) = 0 x =

Automatic growth of a vector: –If variable is not yet vector, assigning element beyond 1 defaults to creating a row vector >>clear xx >>clear xx »xx(4) = 12% xx starts as a 4-element row vector xx =

Warning! Use clear to Avoid Errors Use clear to Avoid Errors –Be careful: suppose you simply want A to have u and v as columns: »A(:,1) = u; »A(:,2) = v; –A however, still has its previous columns! –Use clear to avoid this: »clear A

Vector and Matrix Functions CommandDescription cat(n,A,B,C, …) concatenates A,B,C,… along dimension n find(x) indices of nonzero elements of array x length(A) vector  # of elements, matrix  max. of m or n max(A) matrix  largest elements of each column [x,k] = max(A) stores max.’s in x, indices in k min(A) same as max but minimum values [x,k] = min(A) same as [x,k] = max(A) but minimum values size(A) returns dimensions of array A sort(A) sorts each column in ascending order sum(A) returns vector of column sums

Array Operations Same operation performed on each corresponding element of array A = >> A+3 ans = >> 2*A ans = >> A.^2 ans = B = >> A+B ans = >> A.*B ans = A./B ans =

Math Functions Automatically Apply to Each Element A = >> sin(A) ans = >> sqrt(A) ans = i

Vector Operations u = v = >> dot(u,v) ans = 8 >> cross(u,v) ans = >> norm(u) ans = >> %this is the Euclidean length or magnitude

Matrix Product C= A*B is defined if and only if the number of columns in A equals the number of rows in B. Then C will have the same number of rows as A and the same number of columns as B (3x2)*(2x4) produces a 3x4 (3x2)*(3x2) not defined (3x2)*(2x3) produces a 3x3 (2x3)*(3x2) produces a 2x2

Matrix Product C=A*B The (i,j) entry in C is the sum of the products of entries from the i-th row of A and the j-th column of B A = B = >> A*B ans =

Matrix Power A^n means multiply A by itself n times. A^n means multiply A by itself n times. –Can only be done if A is square A.^n means raise each element of A to the n-th power A.^n means raise each element of A to the n-th power –Can be done with any array A

Matrix Division If A and B are matrices, A/B is NOT a defined operation in linear algebra! If A and B are matrices, A/B is NOT a defined operation in linear algebra! Matlab uses both right (/) and left (\) operator symbols for special matrix operations we are not covering in this course! Matlab uses both right (/) and left (\) operator symbols for special matrix operations we are not covering in this course! 1./A is an array operation creating a new array with each entry being the reciprocal of the entry in A 1./A is an array operation creating a new array with each entry being the reciprocal of the entry in A A./B is an array operation creating a new array with each entry being the corresponding entry in A divided by the corresponding entry in B A./B is an array operation creating a new array with each entry being the corresponding entry in A divided by the corresponding entry in B

Special Matrices eye(n)—nxn “identity” matrix eye(n)—nxn “identity” matrix –eye(3) ones(m,n)– mxn matrix with all entries = 1 ones(m,n)– mxn matrix with all entries = 1 zeros(m,n)—mxn matrix with all entries =0 zeros(m,n)—mxn matrix with all entries =0