Introduction to MATLAB [Vectors and Matrices] Lab 2

Slides:



Advertisements
Similar presentations
Introduction to Matlab
Advertisements

Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
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.
Maths for Computer Graphics
Introduction to Matlab By: Dr. Maher O. EL-Ghossain.
CIS 101: Computer Programming and Problem Solving Lecture 2 Usman Roshan Department of Computer Science NJIT.
Ch 7.2: Review of Matrices For theoretical and computation reasons, we review results of matrix theory in this section and the next. A matrix A is an m.
EGR 106 – Week 3 – More on Arrays Brief review of last week Additional ideas: – Special arrays – Changing an array – Some array operators – Character arrays.
Row 1 Row 2 Row 3 Row m Column 1Column 2Column 3 Column 4.
1 Introduction to MATLAB MATLAB is all of the following: 1.Computational environment 2.Plotting software 3.Programming language Typical applications: 1.Calculations.
Matlab tutorial course Lesson 2: Arrays and data types
Martin Ellison University of Warwick and CEPR Bank of England, December 2005 Introduction to MATLAB.
Introduction to Matlab 1. Outline: What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators Plotting Flow Control Using of M-File Writing.
1 Week 3: Vectors and Matrices (Part III) READING: 2.2 – 2.4 EECS Introduction to Computing for the Physical Sciences.
CSE123 Lecture 5 Arrays and Array Operations. Definitions Scalars: Variables that represent single numbers. Note that complex numbers are also scalars,
Row 1 Row 2 Row 3 Row m Column 1Column 2Column 3 Column 4.
Working with Arrays in MATLAB
Introduction to Matlab. What is Matlab? A software environment for interactive numerical computations Examples:  Matrix computations and linear algebra.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
Chapter 2 Creating Arrays Legend: MATLAB command or syntax : Blue MATLAB command OUTPUT : LIGHT BLUE.
(The Transpose Operator) 1 >> C=[ ; ; ] C = >> D=C' D =
Matrices: Simplifying Algebraic Expressions Combining Like Terms & Distributive Property.
Matrix Operations.
Introduction to Matlab
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
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.
Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.
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.
1 Introduction to Matlab. 2 What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for.
Relational and Logical Operators EE 201 1C7-2 Spring 2012.
Ch. 12 Vocabulary 1.) matrix 2.) element 3.) scalar 4.) scalar multiplication.
LAB 2 Vectors and Matrices Dr.Abdel Fattah FARES.
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators.
Introduction To MATLAB
ECE 1304 Introduction to Electrical and Computer Engineering
Matrix Arithmetic Prepared by Vince Zaccone
Introduction to Matlab
Boyce/DiPrima 10th ed, Ch 7.2: Review of Matrices Elementary Differential Equations and Boundary Value Problems, 10th edition, by William E. Boyce and.
Linear Algebra review (optional)
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
Chapter 2 Creating Arrays.
Chapter 3 Arrays and Vectors
Matrix Operations.
Matrix Operations.
INTRODUCTION TO BASIC MATLAB
Introduction to Matlab
Matlab Workshop 9/22/2018.
StatLab Matlab Workshop
WarmUp 2-3 on your calculator or on paper..
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
7.3 Matrices.
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
Matlab tutorial course
Vectorized Code, Logical Indexing
Vectors and Matrices I.
محاسبات عددی در مهندسی پزشکی جلسه اول و دوم مقدمه ای بر نرم افزار MATLAB گلناز بغدادی 1391.
Digital Image Processing
INTRODUCTION TO MATLAB
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
Announcements P3 due today
3.5 Perform Basic Matrix Operations
Matlab Training Session 2: Matrix Operations and Relational Operators
Linear Algebra review (optional)
Math review - scalars, vectors, and matrices
EECS Introduction to Computing for the Physical Sciences
Working with Arrays in MATLAB
Announcements.
Announcements.
Announcements Exam 2 Next Week!! Final Exam :
Presentation transcript:

Introduction to MATLAB [Vectors and Matrices] Lab 2

Vectors and Matrices Vectors (arrays) are defined as >> w = [1; 2; 4; 5] Matrices (2D arrays) defined similarly >> A = [1,2,3;4,-5,6;5,-6,7]

Vectors and Matrices a vector x = [1 2 5 1] x = 1 2 5 1 1 2 5 1 a matrix x = [1 2 3; 5 1 4; 3 2 -1] 1 2 3 5 1 4 3 2 -1 transpose y = x’ y = 1 5 3 2 1 2 3 4 -1

Vectors and Matrices t =1:10 t = 1 2 3 4 5 6 7 8 9 10 k =2:-0.5:-1 k = 1 2 3 4 5 6 7 8 9 10 k =2:-0.5:-1 k = 2 1.5 1 0.5 0 -0.5 -1 B = [1:4; 5:8] x = 1 2 3 4 5 6 7 8

Arithmetic Operator & Their Precedence Computing with MATLAB Operations Operators Examples Addition Subtraction Multiplication Right Division Left Division Exponentiation + − * / \ ^ >> 𝟓 +𝟑 >> 𝟓 − 𝟑 >> 𝟓∗𝟑 >> 𝟓/𝟑 >> 𝟓\𝟑 = 𝟑/𝟓 >> 𝟓^𝟑 (means 𝟓 𝟑 =𝟏𝟐𝟓) Precedence Order Operators 1 2 3 4 Parentheses ( ). For nested parentheses, the innermost are executed first. Exponentiation, ^ Multiplication, *; Division, /,\ Addition, +; Subtraction, -

Generating Vectors from functions zeros(M,N) MxN matrix of zeros >> zeros(5,1); ones(M,N) MxN matrix of ones rand(M,N) MxN matrix of uniformly distributed random numbers on (0,1) A=rand(5); B=rand(1,5); C=rand(5,1) a=randn(5); b=randn(1,5); c=randn(5,1) x = zeros(1,5) x = 0 0 0 0 0 x = ones(1,3) 1 1 1 x = rand(1,3) 0.9501 0.2311 0.6068

Matrix Operators A and a are two different variables All common operators are overloaded >> v + 2 Common operators are available >> B = A’ >> A*B >> A+B Note: Matlab is case-sensitive A and a are two different variables Transponate conjugates complex entries; avoided by >> B=A’

Indexing Matrices Index complete row or column using the colon operator >> A(1,:) Can also add limit index range >> A(1:2,:) >> A([1 2],:) General notation for colon operator >> v=1:5 >> w=1:2:5

Indexing Matrices A(:,n) A(n,:) A(:,m:n) A(m:n,:) A(m:n,p:q) Addressing vector : (colon operator) Addressing matrix : (colon operator) A(:,n) Refers to the elements in all the rows of column n of the matrix A. A(n,:) Refers to the elements in all the columns of row n of the matrix A. A(:,m:n) Refers to the elements in all the rows between columns m and n of the matrix A. A(m:n,:) Refers to the elements in all the columns between rows m and n of the matrix A. A(m:n,p:q) Refers to the elements in rows m through n and columns p through q of the matrix A.

Matrix information commands Try them…by typing >> help size >> help length >> help ndims

Matrix Index Given: A(-2), A(0) The matrix indices begin from 1 (not 0 (as in C)) The matrix indices must be positive integer Given: A(-2), A(0) Error: ??? Subscript indices must either be real positive integers or logicals. A(4,2) Error: ??? Index exceeds matrix dimensions.

Matrix Index 3 11 6 5 Address 4 7 10 2 13 9 0 8 MAT = 11 6 5 4 7 10 2 3 11 6 5 4 7 10 2 13 9 0 8 MAT = Address (1,1) (1,2) (1,3) (1,4) (2,1) (2,2) (2,3) (2,4) (3,1) (3,2) (3,3) (3,4) 11 6 5 4 7 10 2 9 0 8 MAT =

Commands for building arrays and Matrices

Concatenation of Matrices x = [1 2], y = [4 5], z=[ 0 0] A = [ x y] 1 2 4 5 B = [x ; y] 1 2 4 5 You can try ‘cat’ command as well… for concatenation C = [x y ;z] Error: ??? Error using ==> vertcat CAT arguments dimensions are not consistent.

Matrices Operations Given A and B: Addition Subtraction Product Transpose

Operation on Matrices

Operators (Element by Element) .* element-by-element multiplication ./ element-by-element division .^ element-by-element power

The use of “.” – “Element” Operation 1 2 3 5 1 4 3 2 -1 b = x .* y b= 3 8 -3 c = x . / y c= 0.33 0.5 -3 d = x .^2 d= 1 4 9 x = A(1,:) x= 1 2 3 y = A(:,3) y= 3 4 -1 K= x^2 Erorr: ??? Error using ==> mpower Matrix must be square. B=x*y ??? Error using ==> mtimes Inner matrix dimensions must agree.

Example of element wise operation Most elementary functions, such as sin, exp, etc. act as elementwise

Reducing functions….try them

Multi-dimensional matrices 5 7 4 5 5 7 4 Scalar of 1 X 1 Row Vector of 1 X 3 Column vector of 3 X 1 14 12 10 56 10 504 4 6 89 0 14 12 10 56 6 10 56 4 6 78 86 14 12 10 56 89 10 56 85 23 4 6 23 2 78 86 53 6 10 56 85 23 4 6 23 2 78 86 53 6 One Dimensional Matrix of 3 X 3 Two Dimensional Matrix of 3 X 5 Three Dimensional Matrix of 3 X 5