Mathematical Operations with Arrays and Matrices

Similar documents
Introduction to MATLAB for Engineers, Third Edition

Introduction to Engineering gii

Introduction to MATLAB 7 for Engineers

Introduction to MATLAB. CS534 Fall 2016

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

Introduction to Matlab

Introduction to MATLAB

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

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

Introduction to MatLab. Introduction to MatLab K. Craig 1

Lecture 2: Variables, Vectors and Matrices in MATLAB

Introduction to MATLAB Programming

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

MATLAB Basics EE107: COMMUNICATION SYSTEMS HUSSAIN ELKOTBY

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

Dr Richard Greenaway

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

A 30 Minute Introduction to Octave ENGR Engineering Mathematics Tony Richardson

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

x = 12 x = 12 1x = 16

Introduction to MATLAB

PART 1 PROGRAMMING WITH MATHLAB

ENGR 1181 MATLAB 4: Array Operations

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

A very brief Matlab introduction

ARRAY VARIABLES (ROW VECTORS)

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

A Guide to Using Some Basic MATLAB Functions

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

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

MATLAB Tutorial. Digital Signal Processing. Course Details. Topics. MATLAB Environment. Introduction. Digital Signal Processing (DSP)

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

Lecturer: Keyvan Dehmamy

One-dimensional Array

SF1901 Probability Theory and Statistics: Autumn 2016 Lab 0 for TCOMK

Introduction to MATLAB Practical 1

CME 192: Introduction to Matlab

Introduction to Matlab

Introduction to Matlab. By: Hossein Hamooni Fall 2014

CDA6530: Performance Models of Computers and Networks. Chapter 4: Using Matlab for Performance Analysis and Simulation

Lecture 2. Arrays. 1 Introduction

How to learn MATLAB? Some predefined variables

Chapter 1 Introduction to MATLAB

Introduction to Matlab

CDA5530: Performance Models of Computers and Networks. Chapter 8: Using Matlab for Performance Analysis and Simulation

Topic 3. Mathematical Operations Matrix & Linear Algebra Operations Element-by-Element(array) Operations

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

Fall 2014 MAT 375 Numerical Methods. Introduction to Programming using MATLAB

CDA6530: Performance Models of Computers and Networks. Chapter 4: Using Matlab for Performance Analysis and Simulation

Dr Richard Greenaway

Part #1. A0B17MTB Matlab. Miloslav Čapek Filip Kozák, Viktor Adler, Pavel Valtr

Digital Image Analysis and Processing CPE

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

Lab 1 - Worksheet Spring 2013

1 Introduction to Matlab

ELEMENTARY MATLAB PROGRAMMING

Inlichtingenblad, matlab- en simulink handleiding en practicumopgaven IWS

MATLAB Tutorial Matrices & Vectors MATRICES AND VECTORS

What is MATLAB? What is MATLAB? Programming Environment MATLAB PROGRAMMING. Stands for MATrix LABoratory. A programming environment

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

AN INTRODUCTION TO MATLAB

AH Matrices.notebook November 28, 2016

AMS 27L LAB #1 Winter 2009

Objectives. 1 Running, and Interface Layout. 2 Toolboxes, Documentation and Tutorials. 3 Basic Calculations. PS 12a Laboratory 1 Spring 2014

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

Maths for Signals and Systems Linear Algebra in Engineering. Some problems by Gilbert Strang

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

Matlab and Octave: Quick Introduction and Examples 1 Basics

Matlab Tutorial. Get familiar with MATLAB by using tutorials and demos found in MATLAB. You can click Start MATLAB Demos to start the help screen.

Computer Packet 1 Row Operations + Freemat

1 Overview of the standard Matlab syntax

1. Register an account on: using your Oxford address

ECON 502 INTRODUCTION TO MATLAB Nov 9, 2007 TA: Murat Koyuncu

Introduction to MATLAB. Computational Probability and Statistics CIS 2033 Section 003

This is a basic tutorial for the MATLAB program which is a high-performance language for technical computing for platforms:

MATLAB An introductory tutorial

Introduction and MATLAB Basics

Chapter 2. MATLAB Fundamentals

AMATH 352: MATLAB Tutorial written by Peter Blossey Department of Applied Mathematics University of Washington Seattle, WA

GTPS Curriculum 5 th Grade Math. Topic: Topic 1 - Understand Place Value

1 Introduction to MATLAB

LAB 2: Linear Equations and Matrix Algebra. Preliminaries

Laboratory 1 Octave Tutorial

AMS 27L LAB #2 Winter 2009

Variable Definition and Statement Suppression You can create your own variables, and assign them values using = >> a = a = 3.

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

Matrix Inverse 2 ( 2) 1 = 2 1 2

Introduction to MATLAB

McTutorial: A MATLAB Tutorial

Here is a quick introduction to Matlab and a couple of its symbolic and control functions.

Matrices 4: use of MATLAB

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

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

6.094 Introduction to MATLAB January (IAP) 2009

1 Introduction to MATLAB

2.0 MATLAB Fundamentals

MATLAB SUMMARY FOR MATH2070/2970

Wits Maths Connect Secondary Project Card-sorting Activities

Introduction to Matlab

Math 340 Fall 2014, Victor Matveev. Binary system, round-off errors, loss of significance, and double precision accuracy.

Transcription:

Mathematical Operations with Arrays and Matrices

Array Operators (element-by-element) (important) + Addition A+B adds B and A - Subtraction A-B subtracts B from A.* Element-wise multiplication.^ Element-wise power./ Right array division.\ Left array division.' Array transpose A.*B is the element-by-element product of A and B. A.^B is the matrix with elements A(i,j) to the B(i,j) power. A./B is the matrix with elements A(i,j)/B(i,j). A.\B is the matrix with elements B(i,j)/A(i,j). A.' is the array transpose of A. For complex matrices, this does not involve conjugation.

Matrix Operators * Matrix multiplication / Matrix right division C = A*B is the linear algebraic product of the matrices A and B. The number of columns of A must equal the number of rows of B. x = B/A is the solution to the equation xa = B. Matrices A and B must have the same number of columns. In terms of the left division operator, B/A = (A'\B')'. Matrix left division x = A\B is the solution to the equation Ax = B. Matrices \ A and B must have the same number of rows. Matrix power A^B is A to the power B, if B is a scalar. ^ ' Complex conjugate transpose A' is the linear algebraic transpose of A. For complex matrices, this is the complex conjugate transpose.

Element by Element Math Operations For arrays of identical sizes, addition is defined term by term: the command F = A + B means F(r,c) = A(r,c) + B(r,c) for all row and column pairs r,c element-by-element or elementwise addition

Array Addition and Subtraction a a 11 21 a a 12 22 b b 11 21 b b 12 22 a a 11 21 b b 11 21 a a 12 22 b 12 b 22 Array subtraction is performed in a similar way.

Notes: Arrays must be of identical sizes When a scalar (number) is added to an array, the number is added to all elements of the array. Subtraction is identical

Array element-by-element Mult, Div, Exp (important) The other basic math operations work element by element using the dot notation (with A,B the same sizes): multiplication division F = A.* B F(r,c) = A(r,c).* B(r,c) F = A./ B F(r,c) = A(r,c). / B(r,c) exponentiation: F = A.^ B F(r,c) = A(r,c). ^ B(r,c) note periods (dots)!

For example, the array multiplication operation A.*B results in a matrix C that has the same size as A and B and has the elements c i j = a i j b i j. For example, if then C = A.*B gives this result: a a 11 21 a a 12 22 b.* b 11 21 b b 12 22 a11b 11 a12 b 12 a21 b21 a22 b22

Other examples:

One could be scalar: a = [ 1 2 3 ] b = 2

Element-by-element Operations Symbol Operation Form Examples + Scalar-array addition A + b [6,3]+2=[8,5] - Scalar-array subtraction A b [8,3]-5=[3,-2] + Array addition A + B [6,5]+[4,8]=[10,13] - Array subtraction A B [6,5]-[4,8]=[2,-3].* Array multiplication A.*B [3,5].*[4,8]=[12,40]./ Array right division A./B [2,5]./[4,8]=[2/4,5/8].\ Array left division A.\B [2,5].\[4,8]=[2\4,5\8].^ Array exponentiation A.^B [3,5].^2=[3^2,5^2] Note dots (.) 2.^[3,5]=[2^3,2^5] [3,5].^[2,4]=[3^2,5^4]

Important Built-in functions also work element-by-element: log and exp trigonometric etc.

The built-in MATLAB functions such as sqrt(x) and exp(x) automatically operate on array arguments to produce an array result the same size as the array argument x. Thus these functions are said to be vectorized functions. For example, in the following session the result y has the same size as the argument x. >>x = [4, 16, 25]; >>y = sqrt(x) y = 2 4 5

However, when multiplying or dividing these functions, or when raising them to a power, you must use element-by-element operations if the arguments are arrays. For example, if x is a vector, then to compute z = sqrt(x) sin(x), you must type: z = sqrt(x).*sin(x)

We can raise a scalar to an array power. For example, if p = [2, 4, 5], then typing 3.^p produces the array [3 2, 3 4, 3 5 ] = [9, 81, 243]. Remember that.^ is a single symbol. The dot in 3.^p is not a decimal point associated with the number 3. The following operations, with the value of p given here, are equivalent and give the correct answer: 3.^p 3.0.^p 3..^p (3).^p 3.^[2,4,5]

Exercise (try this out) The range of a projectile is range = v2 sin(2θ) g where v = 750 cm/s and g = 8.91 m/s 2 a) Create a column vector, theta, of angles, 0-90 degrees, by increments of 5 degrees. b) Compute a vector, range, from the formula above; Note that v and g are not in compatible units. c) Display the 10 th element of range d) Plot range vs theta with the command >> plot (theta, range, * )

Array Functions There are many array functions sum, mean, median, min, max, sort, std, etc.

Different Return Values Functions may return different results depending on whether the input argument is a vector or array >> A = [1 2 3]; >> max(a) ans = 3 >> A = [1 2 3; 4 5 6]; >> max(a) ans = 4 5 6 This does the obvious thing returns the maximum element This is not obvious It computes the max value in each column, and returns a row vector.

Multiple Return Values Functions may return different results depending on whether the input argument is a vector or array >> A = [1 2 3; 4 5 6]; >> max(a) ans = 4 5 6 >> [m,i] = max(a) m = i = 4 5 6 2 2 2 A single return variable (row vector) 2 return arguments, maximum row vector and index row vector Note the syntax for returning multiple arguments

>> A = [1 2 6; 4 5 3]; >> max(a) ans = 4 5 6 % max() returns the max number of each column. Then how to find out the single max number in this case? Then how do you find out the single max in the whole matrix. [value,pos]=max(a); %return max of each column and the row number of that max value. finalmax=max(value) % given that the max values are all on the second row, just pick up one of the row value, and find the max value.

An Image processing example: 1. Download pepper.png from the website. 2. Type: RGB=imread( pepper.png ); % read the figure and assign a variable to it. imshow(rgb); % plot the figure named RGB I=rgb2gray(RGB); % RGB to gray conversion figure % open a new window for a new figure Imshow(I); % plot the converted gray figure II=I(500:1000,500:1000); % you can crop part of the figure by select a range from it. figure imshow(ii); BW = edge(i,'sobel ); % use Sobel method to do edge detection Imshow(BW);

Why edge detection? So many applications. Here is one example: