MATLAB Tutorial Matrices & Vectors MATRICES AND VECTORS

Similar documents
OUTLINES. Variable names in MATLAB. Matrices, Vectors and Scalar. Entering a vector Colon operator ( : ) Mathematical operations on vectors.

MATH (CRN 13695) Lab 1: Basics for Linear Algebra and Matlab

EGR 111 Introduction to MATLAB

ENGR 1181 MATLAB 02: Array Creation

Matlab notes Matlab is a matrix-based, high-performance language for technical computing It integrates computation, visualisation and programming usin

MATLAB Basics. Configure a MATLAB Package 6/7/2017. Stanley Liang, PhD York University. Get a MATLAB Student License on Matworks

6.094 Introduction to MATLAB January (IAP) 2009

Physics 326G Winter Class 2. In this class you will learn how to define and work with arrays or vectors.

Dr Richard Greenaway

Matlab Tutorial. The value assigned to a variable can be checked by simply typing in the variable name:

2.0 MATLAB Fundamentals

A Guide to Using Some Basic MATLAB Functions

Introduction to MATLAB

EE 301 Signals & Systems I MATLAB Tutorial with Questions

Course Layout. Go to follow instr. Accessible within campus (only for the first download)

Chapter 2. MATLAB Fundamentals

MATLAB for beginners. KiJung Yoon, 1. 1 Center for Learning and Memory, University of Texas at Austin, Austin, TX 78712, USA

ARRAY VARIABLES (ROW VECTORS)

FreeMat Tutorial. 3x + 4y 2z = 5 2x 5y + z = 8 x x + 3y = -1 xx

Introduction to Engineering gii

AN INTRODUCTION TO MATLAB

Identity Matrix: >> eye(3) ans = Matrix of Ones: >> ones(2,3) ans =

Introduction to MATLAB for Engineers, Third Edition

University of Alberta

MATLAB TUTORIAL WORKSHEET

Matrices. A Matrix (This one has 2 Rows and 3 Columns) To add two matrices: add the numbers in the matching positions:

Lab of COMP 406. MATLAB: Quick Start. Lab tutor : Gene Yu Zhao Mailbox: or Lab 1: 11th Sep, 2013

A very brief Matlab introduction

Introduction to MATLAB

How to Use MATLAB. What is MATLAB. Getting Started. Online Help. General Purpose Commands

PowerPoints organized by Dr. Michael R. Gustafson II, Duke University

MATLAB Fundamentals. Berlin Chen Department of Computer Science & Information Engineering National Taiwan Normal University

Vector: A series of scalars contained in a column or row. Dimensions: How many rows and columns a vector or matrix has.

To start using Matlab, you only need be concerned with the command window for now.

(Creating Arrays & Matrices) Applied Linear Algebra in Geoscience Using MATLAB

An Introduction to Numerical Methods

Vectors and Matrices. Chapter 2. Linguaggio Programmazione Matlab-Simulink (2017/2018)

Introduction to MATLAB 7 for Engineers

Getting Started with MATLAB

Introduction to Matlab. By: Dr. Maher O. EL-Ghossain

Experiment 1: Introduction to MATLAB I. Introduction. 1.1 Objectives and Expectations: 1.2 What is MATLAB?

Array Creation ENGR 1181 MATLAB 2

Introduction to MATLAB

An Introduction to MATLAB II

Introduction to MatLab. Introduction to MatLab K. Craig 1

1 Introduction to Matlab

Introduction to Matlab

Matlab and Vectors. next page. close. exit. Math 45 Linear Algebra. David Arnold.

Introduction to Matlab

EGR 102 Introduction to Engineering Modeling. Lab 05A Managing Data

TUTORIAL 1 Introduction to Matrix Calculation using MATLAB TUTORIAL 1 INTRODUCTION TO MATRIX CALCULATION USING MATLAB

Colorado State University Department of Mechanical Engineering. MECH Laboratory Exercise #1 Introduction to MATLAB

How to learn MATLAB? Some predefined variables

PART 1 PROGRAMMING WITH MATHLAB

Eric W. Hansen. The basic data type is a matrix This is the basic paradigm for computation with MATLAB, and the key to its power. Here s an example:

Computer Packet 1 Row Operations + Freemat

George Mason University ECE 201: Introduction to Signal Analysis Spring 2017

Mathematical Operations with Arrays and Matrices

MATLAB Lesson I. Chiara Lelli. October 2, Politecnico di Milano

Introduction to Matlab

Performing Matrix Operations on the TI-83/84

ELEC4042 Signal Processing 2 MATLAB Review (prepared by A/Prof Ambikairajah)

One-dimensional Array

STAT/MATH 395 A - PROBABILITY II UW Winter Quarter Matlab Tutorial

x = 12 x = 12 1x = 16

Matrices 4: use of MATLAB

Interactive Computing with Matlab. Gerald W. Recktenwald Department of Mechanical Engineering Portland State University

Armstrong State University Engineering Studies MATLAB Marina 2D Arrays and Matrices Primer

Introduction to Matlab

LAB 2 VECTORS AND MATRICES

A = [1, 6; 78, 9] Note: everything is case-sensitive, so a and A are different. One enters the above matrix as

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

Introduction to MATLAB

CME 192: Introduction to Matlab

Introduction to MATLAB Programming

Introduction to Matlab

Array Creation ENGR 1187 MATLAB 2

1 Overview of the standard Matlab syntax

Lecture 2: Variables, Vectors and Matrices in MATLAB

ELEMENTARY MATLAB PROGRAMMING

MATLAB Project: Getting Started with MATLAB

Matlab Introduction. Scalar Variables and Arithmetic Operators

MATLAB GUIDE UMD PHYS401 SPRING 2012

Unix Computer To open MATLAB on a Unix computer, click on K-Menu >> Caedm Local Apps >> MATLAB.

EE3TP4: Signals and Systems Lab 1: Introduction to Matlab Tim Davidson Ext Objective. Report. Introduction to Matlab

Introduction to MATLAB LAB 1

PHYS 210: Introduction to Computational Physics MATLAB Exercises 2

Matrix Inverse 2 ( 2) 1 = 2 1 2

Introduction to MATLAB Practical 1

Grace days can not be used for this assignment

Lab #1 Revision to MATLAB

Chapter 1 Introduction to MATLAB

Systems of Inequalities and Linear Programming 5.7 Properties of Matrices 5.8 Matrix Inverses

ELEN E3084: Signals and Systems Lab Lab II: Introduction to Matlab (Part II) and Elementary Signals

A. Matrix-wise and element-wise operations

MATLAB Project: Getting Started with MATLAB

Introduction to Matlab

Matlab- Command Window Operations, Scalars and Arrays

Creates a 1 X 1 matrix (scalar) with a value of 1 in the column 1, row 1 position and prints the matrix aaa in the command window.

EEE161 Applied Electromagnetics Laboratory 1

Transcription:

MATRICES AND VECTORS A matrix (m x n) with m rows and n columns, a column vector (m x 1) with m rows and 1 column, and a row vector (1 x m) with 1 row and m columns all can be used in MATLAB. Matrices and vectors, when defined, are placed in brackets. To create a row vector simply use a space to separate elements. To create a column vector separate the elements with a semicolon. >> p=[1 4 6 7 8] p = 1 4 6 7 8 >> q=[1;2;5;1;6;] q = 1 2 5 1 6 Vectors can be multiplied, but remember for vectors order of multiplication is important. For our vectors, p*q will yield a 1 x 1 matrix, a.k.a. a scalar, and q*p will yield a 5 x 5 matrix. >> r=p*q r = 94 >> s=q*p s = 1 4 6 7 8 2 8 12 14 16 5 20 30 35 40 1 4 6 7 8 6 24 36 42 48 Remember that the inner dimensions must agree. A 5 x 1 matrix cannot be multiplied by a 5 x 1 matrix. However, in some cases instead of performing a vector multiplication, we ES 111 1/6

might want to multiply individual elements in two vectors by each other. We can do this by using the.* notation. For this notation, the two vectors must have the same dimensions. >> t=p.*q??? Error using ==>.* Matrix dimensions must agree. >> t=p.*q' t = 1 8 30 7 48 Note that the apostrophe after q transposed q, converting q from a 5 x 1 to a 1 x 5 vector. For addition and subtraction, the vectors must have the same dimensions. >> p-q??? Error using ==> - Matrix dimensions must agree. >> p-t 0-4 -24 0-40 >> p+q??? Error using ==> + Matrix dimensions must agree. >> p+t 2 12 36 14 56 One tricky operation is the square function. This must be performed element by element for a vector.^ instead of just ^ because the ^ notation requires a square matrix. >> p^2??? Error using ==> ^ Matrix must be square. >> p.^2 ES 111 2/6

1 16 36 49 64 For functions that require an argument in parentheses, e.g. abs( ), sqrt( ), etc., it will perform the operations element by element. >> sqrt(p) 1.0000 2.0000 2.4495 2.6458 2.8284 Matrices are made up of several vectors put together. A matrix can be defined as a collection of row vectors. >> A=[1 3 5; 2 1 7; 9 0 2;] A = 1 3 5 2 1 7 9 0 2 To extract elements from A, simply use A(row,column). To obtain the element in the second row and third column, do the following. >> A(2,3) 7 You can also extract parts or entire rows and columns with the colon notation. The general form to extract all elements from the a th row to the b th row and the c th column to the d th column is A(a:b,c:d). For a=2, b=3, c=1, & d=2, we get. >> A(2:3,1:2) 2 1 9 0 ES 111 3/6

Operations with scalars can be performed with ease as well. For example, subtracting, adding, multiplying, etc. can be done with a scalar. >> A-1 0 2 4 1 0 6 8-1 1 >> A*3 3 9 15 6 3 21 27 0 6 An individual element in A can be redefined too. Simply redefine that single element and the others will remain unchanged. >> A(2,2)=99 A = 1 3 5 2 99 7 9 0 2 In this case, the second row, second column element was the only one that changed. There are two other ways to define vectors, which are particularly helpful when making plots. The first is linspace(first,last,n), which creates a row vector with the first element equal to first, the last element equal to last, and n total elements in the vector. The second is using colon notation first:increment:last, where a row vector is created with the first element equal to first and each subsequent element being an increment greater than the previous until the last one, which is less than or equal to last. If increment is left out, MATLAB will use a default value of 1. >> linspace(2,10,5) 2 4 6 8 10 ES 111 4/6

>> 2:5:10 2 7 >> 2:4:10 2 6 10 Notice the difference between the two. Again this is helpful for plotting. Let s say you are going to make a plot of the sine function. You need to define an argument for the sine function in MATLAB. Let s look at the sine function from 0 to 2pi with increments of pi/4. >> x=linspace(0,2*pi,9) x = 0 0.7854 1.5708 2.3562 3.1416 3.9270 4.7124 5.4978 6.2832 >> y=sin(x) y = 0 0.7071 1.0000 0.7071 0.0000-0.7071-1.0000-0.7071-0.0000 >> z=cos(x) z = 1.0000 0.7071 0.0000-0.7071-1.0000-0.7071-0.0000 0.7071 1.0000 EXAMPLE 1) Using colon notation and the linspace command make vectors that: a. Go from 0 to 10 with an increment of 2 b. Go from 4 to 10 with an 7 elements a.) The increment is given so the colon notation should be straight forward. For the linspace command there should be (10-0)/2+1 elements, which is 6. ES 111 5/6

>> a1=0:2:10; >> a2=linspace(0,10,6); b.) Here the number of elements is given, so the linspace command should be straightforward. The increment is (10-4)/(7-1), which is one. >>b1=4:10; >>b2=linspace(4,10,7); 2) If A is a 4 x 1 column vector and B is a 1 x 4 row vector, what is the size of the matrix resulting from the following calculations: a) A*B b) B*A c) A.*B d) A^2 e) A.^2 3) If C is a 4 x 4 matrix and D is a 2 x 4 matrix, what is the result of the following calculations: a) C*D b) D*C c) C.*D DO IT YOURSELF 1) Create a vector using both colon notation and the linspace command that: a) starts from 10 and goes to -10 with increments of 2.5 b) starts from 25 and goes to 45 with 5 elements ES 111 6/6