MATLAB: The Basics. Dmitry Adamskiy 9 November 2011

Similar documents
MATLAB COURSE FALL 2004 SESSION 1 GETTING STARTED. Christian Daude 1

Lecture 2: Variables, Vectors and Matrices in MATLAB

Introduction. Matlab for Psychologists. Overview. Coding v. button clicking. Hello, nice to meet you. Variables

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

MATLAB for Experimental Research. Fall 2018 Vectors, Matrices, Matrix Operations

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

Introduction to MATLAB Programming

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

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

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

LAB 2 VECTORS AND MATRICES

CME 192: Introduction to Matlab

Matlab Tutorial for COMP24111 (includes exercise 1)

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

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

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

Matlab Tutorial and Exercises for COMP61021

Introduction to MATLAB

Outline. CSE 1570 Interacting with MATLAB. Outline. Starting MATLAB. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

Outline. User-based knn Algorithm Basics of Matlab Control Structures Scripts and Functions Help

Chapter 2. MATLAB Fundamentals

Introduction to MATLAB

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

Introduction to Matlab

MATLAB Lecture 1. Introduction to MATLAB

MAT 343 Laboratory 2 Solving systems in MATLAB and simple programming

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

Basics: vectors, matrices, arrays

Teaching Manual Math 2131

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An

Dr Richard Greenaway

Introduction to MATLAB. Simon O Keefe Non-Standard Computation Group

Finding, Starting and Using Matlab

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

EGR 111 Introduction to MATLAB

1 Overview of the standard Matlab syntax

Introduction to MatLab. Introduction to MatLab K. Craig 1

Computational Photonics, Seminar 01 on Introduction into MATLAB, Page 1

CITS2401 Computer Analysis & Visualisation

AN INTRODUCTION TO MATLAB

Matrix Manipula;on with MatLab

MATLAB. Devon Cormack and James Staley

Introduction to MATLAB

A Brief MATLAB Tutorial

McTutorial: A MATLAB Tutorial

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

2.0 MATLAB Fundamentals

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

Desktop Command window

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

Matlab Introduction. Scalar Variables and Arithmetic Operators

Introduction to. The Help System. Variable and Memory Management. Matrices Generation. Interactive Calculations. Vectors and Matrices

Chapter 1 Introduction to MATLAB

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.

Introduction to Engineering gii

University of Alberta

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

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

PART 1 PROGRAMMING WITH MATHLAB

HERIOT-WATT UNIVERSITY DEPARTMENT OF COMPUTING AND ELECTRICAL ENGINEERING. B35SD2 Matlab tutorial 1 MATLAB BASICS

Introduction to Matlab

Introduction to MATLAB

Getting To Know Matlab

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

One-dimensional Array

Introduction to MATLAB

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

A Quick Introduction to MATLAB/Octave. Kenny Marino, Nupur Chatterji

PROGRAMMING AND ENGINEERING COMPUTING WITH MATLAB Huei-Huang Lee SDC. Better Textbooks. Lower Prices.

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

ECE Lesson Plan - Class 1 Fall, 2001

An Introduction to MATLAB See Chapter 1 of Gilat

SMS 3515: Scientific Computing Lecture 1: Introduction to Matlab 2014

Computer Vision. Matlab

Computational Mathematics

MATLAB Tutorial EE351M DSP. Created: Thursday Jan 25, 2007 Rayyan Jaber. Modified by: Kitaek Bae. Outline

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

1. Register an account on: using your Oxford address

Starting with a great calculator... Variables. Comments. Topic 5: Introduction to Programming in Matlab CSSE, UWA

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

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

The Very Basics of the R Interpreter

Appendix A. Introduction to MATLAB. A.1 What Is MATLAB?

A/D Converter. Sampling. Figure 1.1: Block Diagram of a DSP System

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

Lecture 2. Arrays. 1 Introduction

A General Introduction to Matlab

Matlab course at. P. Ciuciu 1,2. 1: CEA/NeuroSpin/LNAO 2: IFR49

Dr Richard Greenaway

Matlab and Psychophysics Toolbox Seminar Part 1. Introduction to Matlab

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

MATLAB GUIDE UMD PHYS401 SPRING 2012

Computer Packet 1 Row Operations + Freemat

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

Introduction to Matlab

Introduction to MATLAB LAB 1

CS129: Introduction to Matlab (Code)

Digital Image Processing

LAB 1: Introduction to MATLAB Summer 2011

Transcription:

MATLAB: The Basics Dmitry Adamskiy adamskiy@cs.rhul.ac.uk 9 November 2011 1

Starting Up MATLAB Windows users: Start up MATLAB by double clicking on the MATLAB icon. Unix/Linux users: Start up by typing matlab at the operating system prompt. 2

Current Directory Workspace The prompt Command Window Command History 3

Creating Variables (1) The names of variables: must begin with a letter but can have numbers later in the name are case sensitive can be up to 32 characters long cannot have spaces or punctuation marks but underscore _ is allowed cannot be reserved words like if 4

Creating Variables (2) To create a variable A with initial value 10, we type: >> A = 10 A = 10 A is now stored as a variable in MATLAB s memory or workspace with a value 10. A now can be used in summations, multiplications etc. 5

Manipulating Variables >> A + A ans = 20 We can even assign the results to a new variable: >> B = A + 5 B = 15 Or even overwrite a value of a variable >> A = A * A A = 100 6

List The Variables (1) >> whos Name Size Bytes Class A 1x1 8 double array B 1x1 8 double array ans 1x1 8 double array Grand total is 3 elements using 24 bytes 7

List The Variables (2) Name : the name you use to refer to the variable Size: the number of rows (first number) and columns (second number) Bytes: how much memory the variable uses Type: all numbers are double arrays by default, but you can also use text, cell and logical 8

Clear Variables To clear a particular variable A type in: >> clear A To clear all the variables in the workspace type in: >> clear all 9

Suppressing the output Example: >> A = 20 A = 20 >> A = 25; >> A A = 25 10

MATLAB : Matrices and Punctuation 11

Arrays Types of arrays: 12 A scalar a single number A = 10 A vector a row or column of numbers B = 1 2 3 A matrix a two-dimensional array C = 1 5 3 4 5 2 7 8 9 3 4 0 A multi-dimensional array an array with more than 2 dimensions.

Vectors To enter most data into MATLAB you need to use square brackets [ ] To specify a row vector RV type in: >> RV = [ 1 2 5 4 ] or >> RV = [ 1, 2, 5, 4 ] To specify a column vector CV type in: 13 >> CV = [ 1 ; 2 ; 5 ; 4 ] In this context a semi colon means start a new line.

Matrices (1) o To specify a matrix M of size 2x3 type >> M = [ 1 2 3 ; 4 5 6 ] M = 1 2 3 4 5 6 To refer to the entries in the matrix we use round brackets () >> M(2,1) ans = 4 Warning: referring to an index outside the matrix bound will produce an error message. >> M(3,4)??? Index exceeds matrix dimensions 14

Matrices (2) You can overwrite the value of an entry in a matrix too. >> M(2,1) = 0 M = 1 2 3 0 5 6 You can add a new column/row to the matrix >> M( 3, : ) = [ 7 8 9 ] M = 1 2 3 0 5 6 7 8 9 Warning: you are only allowed to add a column/row of the same size as the original size of the matrix. 15

The Colon Operator (1) In round brackets colon means everything in a row or column and is normally used to extract data from a matrix. M(2,:) means row 2, every column M(:,3) means every row, column 3 M(:) arrange M into a column vector 16

Example >> M = [1 2 3;4 5 6;7 8 9] M = 1 2 3 4 5 6 7 8 9 >> M (2, :) ans = 4 5 6 >> M (:, 3) ans = 3 6 9 17

The Colon Operator (2) Between the numbers colon means count from A to B one integer at a time >> D = 5 : 10 D = 5 6 7 8 9 10 >> D(:, 3:5) ans = 7 8 9 18

The Colon Operator (3) A set of three numbers separated by colons specifies the step to use for counting. >> E = 1 : 2 : 10 E = 1 3 5 7 9 >> F = 0 : ¼ : 1 F = 0 0.25 0.5 0.75 1 >> G = 10 : -4 : 2 G = 10 6 2 19

Deleting a Row/Column You can delete rows and columns from a matrix using just a pair of square brackets. M(:,2)=[] deletes the second column M(1,:)=[] deletes the first row M(1,2)=[] deletion of a single entry from a matrix results in an error message as the result of this operation is not a matrix anymore. M(4)=[] deletes entry number 4 and reshapes the rest into a row vector. 20

Example >> M = [1 2 3;4 5 6;7 8 9] M = 1 2 3 4 5 6 7 8 9 >> M(2,:)=[] M = 1 2 3 7 8 9 >> M(2)=[] M = 1 2 8 3 9 21

Some special matrices zeros: all zeros ones: all ones rand: uniformly distributed random elements randn: normally distributed random elements 22

Example >> Z = zeros(2,4) Z = 0 0 0 0 0 0 0 0 >> F = ones(1,2) F = 1 1 >> G = 3*ones(2,4) G = 3 3 3 3 3 3 3 3 23

Example Cont d >> R = rand(3, 2) R = 0.9501 0.4860 0.2311 0.8913 0.6068 0.7621 >> N = randn(2, 2) N = -1.1465 1.1892 1.1909-0.0376 24

The Array Editor Click on the workspace tab Double click on an array s name Edit the values you want to change You can copy your data from Excel and paste them here 25

Exercises Magic matrix: A square matrix with the property that the sum of its columns, sum of its rows, sum of the elements of its diagonal are all equal. Type help magic at the prompt to find out more about this. 26

Exercises Cont d Clear the workspace by using clear all first and then type the following magic square at prompt. (is there a quicker way of producing this magic square?) M = 1 6 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 27

Exercises Cont d (A) Extract the element at row 2 column 4 (B) Extract the entire row 4(C) Extract the entire column 2 (D) Extract columns 1 to 3 at the same time (E) Extract rows 2 and 4 at the same time (F) Update entry of row 2 column 3 to 100 (G) Update row 4 to a row of zeros (H) Add a new column of all 1 s to the matrix (I) Add a row of all 9 s to the matrix (J) Delete the entire row 3 (K) Multiply all the element of the last row by 5. (hint: use the end keyword to access the last row) 28

Solutions Let s find them together 29

MATLAB : Basic Maths 30

Arithmetic Operators In MATLAB variables are treated as numbers they represent Expressions use familiar arithmetic operators and precedence rules Matrices can be manipulated using operators and pre-defined functions 31

Arithmetic Operators (1) + addition - subtraction () specify the order of operations transpose (turn rows into columns and vice versa) 32

Arithmetic Operators (2) * matrix multiplication / matrix division.* element-wise multiplication./ element-wise division.^ power For scalars it does not matter which multiplication or division to use. 33

Example (1) >> 5 * 4 ans = 20 >> 6^2 ans = 36 >> A = 3/4 A = 0.7500 >> B = A + 5 / 4 B = 2 34

Example (2) >> C = [ 1 2 3 ]; >> D = 2*C D = 2 4 6 >> E = [ 2 4 ; 5 6 ; 1 2 ]; >> F = D*E F = 30 44 >> G = [ 4 2 1]; >> H = C.*G H = 4 4 3 35

Example (3) >> I = [4 5 6; 3 2 4] I = 4 5 6 3 2 4 >> T = I T = 4 3 5 2 6 4 36

37 Basic Functions

What are functions? Functions are pieces of code written by you or someone else which receive some inputs (arguments) and give you some outputs. All pre-defined function in MATLAB have a help file to help you use them. Use help followed by the name of the function for further details. 38

Functions (1) MATLAB provides a large number of standard elementary mathematical functions, including abs, sqrt, exp and sin. For a list of these elementary functions type: help elfun For a list of advanced mathematical and matrix functions, type: help specfun and help elmat 39

Functions (2) All functions have one or more inputs or arguments and produce one or more outputs. All functions have the form: [output1, output2, ] = function (arg1, arg2, ) Example: M = magic(4); 40

The sum function S = sum(a) is the sum of the elements of vector A. S = sum(a,dim) sums along the dimension DIM. DIM is the dimension and, for a matrix, can either be 1 (which means columns) or 2 (which indicates rows). 41

The diag Function diag(a): returns a column vector of the elements of the main diagonal. A has to be a square matrix. diag(a,n): returns a column vector formed from the elements of the n-th diagonal of the square matrix A. diag(a,0): is the same as diag(a). 42

Magic Square Example >> M = magic(4) M = 1 6 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 >> CS = sum(m,1) CS = 34 34 34 34 >> US = sum(m) US = 34 34 34 34 43

Example Cont d >> RS = sum(m,2) RS = 34 34 34 34 >> first_col = sum(m(:,1)) first_col = 34 >> second_row = sum(m(2,:)) second_row = 34 44

Example Cont d >> D = diag(m) D = 16 11 6 1 >> SD = sum(d) SD = 34 >> SD = sum(diag(m)) SD = 34 45

Example Cont d >> M M = 1 6 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 >> D_2 = diag(m,2) D_2 = 3 8 46

The size Function [M,N]= size(a): returns the size of an MxN matrix A in a row vector. Example: A = 1 2 3 4 >> size(a) ans = 2 2 47

The fliplr Function fliplr(a): returns a new matrix with rows preserved and columns flipped in the left/right direction. Example: A = 1 2 3 4 >> A = fliplr(a) A = 2 1 4 3 48

Example >> s = size(m) s = 4 4 >> M_flipped = fliplr(m) M_flipped = 13 3 2 16 8 10 11 5 12 6 7 9 1 15 14 4 49

Example Cont d >> DiagF = diag(m_flipped) DiagF = 13 10 7 4 >> SumF = sum(daigf) SumF = 34 >> SumF = sum(diag(fliplr(m))) SumF = 34 50

Acknowledgements The material for this handout are taken from MATLAB manuals and http://www.icn.ucl.ac.uk/webspace/us ers/ahamilton/matlab.htm 51